標籤:
自訂控制項首先要有一個布局檔案
<?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項目自訂群組合控制項