android項目自訂群組合控制項

來源:互聯網
上載者:User

標籤:

自訂控制項首先要有一個布局檔案

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="60dp"    android:padding="5dp" >    <TextView        android:id="@+id/tv_down"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/tv_up"        android:textColor="#a000"        android:textSize="14sp" />    <View        android:id="@+id/view1"        android:layout_width="match_parent"        android:layout_height="0.5dp"        android:background="#a000"         android:layout_alignParentBottom="true"/>    <CheckBox        android:id="@+id/ck_right"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:layout_alignParentRight="true"        android:clickable="false"        android:focusable="false"        android:focusableInTouchMode="false"        />    <TextView        android:id="@+id/tv_up"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:textColor="#000"        android:textSize="22sp" /></RelativeLayout>

然後要寫一個類來繼承一個父布局,

在裡面就可以進行設定

package com.itheima.view;import com.itheima.superman.R;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.CheckBox;import android.widget.RelativeLayout;import android.widget.TextView;public class SeetingView extends RelativeLayout{    private TextView tv_up;    private TextView tv_down;    private CheckBox ck_right;    public SeetingView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        initView();    }    public SeetingView(Context context, AttributeSet attrs) {        super(context, attrs);        initView();    }    public SeetingView(Context context) {        super(context);        initView();    }    //初始化布局    private void initView(){        //給這個布局一個父控制項        View.inflate(getContext(),R.layout.item_seeting, this);        tv_up = (TextView) findViewById(R.id.tv_up);        tv_down = (TextView) findViewById(R.id.tv_down);        ck_right = (CheckBox) findViewById(R.id.ck_right);    }    //設定頂部文字    public void setUp(String text){        tv_up.setText(text);    }    //設定底部文字    public void setDown(String text){        tv_down.setText(text);    }    //是否被選中    public boolean isChecked(){        return ck_right.isChecked();    }    public void setChecked(boolean b){        ck_right.setChecked(b);    }    }

然後就可以在布局中使用自訂的布局了

<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"    android:background="@drawable/home_bg">    <TextView         style="@style/TitleStyle"        android:text="設定中心"        />    <com.itheima.view.SeetingView        android:id="@+id/stv_updata"        android:layout_marginTop="5dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />    </LinearLayout>

之後再activity裡進行操作,和系統控制項一樣

package com.itheima.superman;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import com.itheima.view.SeetingView;public class SeetingActivity extends Activity {    private SeetingView stv_updata;    private SharedPreferences mPreferences;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_seeting);        mPreferences = getSharedPreferences("config", 0);        stv_updata = (SeetingView) findViewById(R.id.stv_updata);        stv_updata.setUp("自動更新設定");        if (mPreferences.getBoolean("check", true)) {            stv_updata.setChecked(true);            stv_updata.setDown("自動更新已經開啟");        } else {            stv_updata.setChecked(false);            stv_updata.setDown("自動更新已經關閉");        }        stv_updata.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                // 選中狀態                if (stv_updata.isChecked()) {                    stv_updata.setChecked(false);                    stv_updata.setDown("自動更新已經關閉");                    mPreferences.edit().putBoolean("check", false).commit();                } else {                    stv_updata.setChecked(true);                    stv_updata.setDown("自動更新已經開啟");                    mPreferences.edit().putBoolean("check", true).commit();                }            }        });    }}

 

android項目自訂群組合控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.