1. 程式人生 > >設定工作時間workday,gravity與layout_gravity區別

設定工作時間workday,gravity與layout_gravity區別

android:gravity 屬性是對該view中內容的限定.比如一個button 上面的text. 你可以設定該text 相對於view的靠左,靠右等位置.

android:layout_gravity是用來設定該view相對與父view 的位置.比如一個button 線上性佈局裡,你想把該button放線上性佈局裡靠左、靠右等位置就可以通過該屬性設定.

效果圖

這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述

程式碼分兩個頁面,第一個是設定頁面xml簡略如下:

<LinearLayout
        android:id="@+id/att_setting_lin_workday"
        android:background
="@color/baseColorWhite" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical" android:orientation="horizontal">
<TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height
="wrap_content" android:layout_marginLeft="10dp" android:textSize="14sp" android:textColor="@color/baseColorBlack" android:text="工作日" />
<TextView android:id="@+id/att_setting_workday" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dp" android:text="週一至週六"/>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:src="@mipmap/right_menu_arrow" /> </LinearLayout>

code:

public class AttendanceSettingActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout linWorkDay;
    private TextView textSettingWork;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attendance_setting);
        initView();
        toListener();

    }

    @Override
    public void initView() {
        linWorkDay = (LinearLayout) findViewById(R.id.att_setting_lin_workday);
        textSettingWork = (TextView) findViewById(R.id.att_setting_workday);
    }

    @Override
    public void toListener() {
        linWorkDay.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.att_setting_lin_workday:
                Intent intent = new Intent();
                intent.setClass(AttendanceSettingActivity.this,WorkDayActivity.class);
                Bundle bundle = new Bundle();
                intent.putExtras(bundle);
                startActivityForResult(intent, 0);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode ==0 && resultCode == Activity.RESULT_OK){
                Bundle bundle = data.getExtras();
              String setWorkDay = bundle.getString("name");
            textSettingWork.setText(setWorkDay);

        }
    }
}

工作日介面

xml如下佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.energy.dingdang.activity.WorkDayActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/baseColorBlue">

        <ImageButton
            android:id="@+id/work_return"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@mipmap/global_arrow" />

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="工作日"
            android:textColor="@color/baseColorWhite"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/work_ensure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:text="確定"
            android:textColor="@color/baseColorWhite"
            android:textSize="16sp"

            />


    </RelativeLayout>

    <LinearLayout
        android:id="@+id/about_to_company"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="星期一"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/work_mon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:button="@drawable/workday_point_selector"
            />
    </LinearLayout>

    <View
        style="@style/RightViewStyle"
         />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="星期二"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/work_tues"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:button="@drawable/workday_point_selector"
            />
    </LinearLayout>

    <View
        style="@style/RightViewStyle"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="星期三"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/work_wednes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:button="@drawable/workday_point_selector"
            />
    </LinearLayout>

    <View
        style="@style/RightViewStyle"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="星期四"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/work_thurs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:button="@drawable/workday_point_selector"
            />
    </LinearLayout>

    <View
        style="@style/RightViewStyle"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="星期五"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/work_fri"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:button="@drawable/workday_point_selector"
            />
    </LinearLayout>

    <View
        style="@style/RightViewStyle"
        />
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="星期六"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/work_satur"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:button="@drawable/workday_point_selector"
            />
    </LinearLayout>

    <View
        style="@style/RightViewStyle"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="星期日"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/work_sun"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:button="@drawable/workday_point_selector"
            />
    </LinearLayout>

    <View
        style="@style/RightViewStyle"
        />

</LinearLayout>

code:如下

public class WorkDayActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton workReturn;
    private TextView textViewEnsure;
    private CheckBox checkBoxMom;
    private CheckBox checkBoxTues;
    private CheckBox checkBoxWednes;
    private CheckBox checkBoxThurs;
    private CheckBox checkBoxFri;
    private CheckBox checkBoxSatur;
    private CheckBox checkBoxSun;
    private boolean[] judge;
    private CheckBox[] checkDay;

    private boolean isContinuous = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_day);
        initView();
        toListener();
    }

    @Override
    public void initView() {
        workReturn = (ImageButton) findViewById(R.id.work_return);
        textViewEnsure = (TextView) findViewById(R.id.work_ensure);

        checkBoxMom = (CheckBox) findViewById(R.id.work_mon);
        checkBoxTues = (CheckBox) findViewById(R.id.work_tues);
        checkBoxWednes = (CheckBox) findViewById(R.id.work_wednes);
        checkBoxThurs = (CheckBox) findViewById(R.id.work_thurs);
        checkBoxFri = (CheckBox) findViewById(R.id.work_fri);
        checkBoxSatur = (CheckBox) findViewById(R.id.work_satur);
        checkBoxSun = (CheckBox) findViewById(R.id.work_sun);
    }

    @Override
    public void toListener() {
        workReturn.setOnClickListener(this);
        textViewEnsure.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.work_return:
                finish();
                break;
            case R.id.work_ensure:


                Intent intent = getIntent();
                Bundle bundle = intent.getExtras();
                bundle.putString("name", JudgeDay());
                intent.putExtras(bundle);
                setResult(Activity.RESULT_OK, intent);
                finish();
                break;

        }
    }

    int indexEnd = 0, indexStart = 0;

    private String JudgeDay() {
        StringBuffer stringBuffer = new StringBuffer();
        String str[] = new String[]{"週一","週二","週三","週四","週五","週六","週日"};
        int i = 0 ;
        checkDay = new CheckBox[]{checkBoxMom, checkBoxTues, checkBoxWednes, checkBoxThurs, checkBoxFri, checkBoxSatur, checkBoxSun};
        judge = new boolean[7];

        for (CheckBox checkBox : checkDay) {
            judge[i] = checkBox.isChecked();
            System.out.println(judge[i] + "Judge");
            i++;
        }
        for ( int k = 0; k < judge.length; k++) {
            System.out.println(judge[k] + "開始Judge" );
            if(judge[k]){
                indexStart = k;
                break;
            }
        }
        for ( int k =judge.length-1; k >=0 ; k--) {
            if(judge[k]){
                indexEnd = k;
                break;
            }
        }

        System.out.println(indexStart + "結果 " + indexEnd);

//判斷不連續
        for (int k = indexStart;k<=indexEnd;k++){
            if (!judge[k]){
                System.out.println(indexStart + "-" + indexEnd+"不連續");
                isContinuous = false;
                break;
            }
        }

        if(isContinuous){
            System.out.println(str[indexStart] + "至" + str[indexEnd]);
            stringBuffer.append(str[indexStart] + "至" + str[indexEnd]);
        }else {
                for ( int k =0; k <judge.length ; k++) {

                     if (judge[k] && k!=indexEnd){
                      stringBuffer.append(str[k]+",");
                     }else if(k == indexEnd) {
                         stringBuffer.append(str[indexEnd]);
                     }
                }
            }

        System.out.println(stringBuffer);
        String  a = stringBuffer.toString();

        return  a;
    }





}