android 自定義控件

目標(biāo): 完成以下類型的組件,要求布局一致,文案通過屬性配置方式完成,且checkbox狀態(tài)變化,需要切換不同的文案

image.png
  • 創(chuàng)建布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" tools:context="com.example.yhb.mobilesafe.activity.SettingActivity"> <TextView android:id="@+id/tv_tltle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="20sp" /> <TextView android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_tltle" android:layout_marginTop="3dp" android:textColor="#7000" android:textSize="18sp" /> <CheckBox android:id="@+id/cb_state" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" /> <View android:layout_width="match_parent" android:layout_height="0.2dp" android:layout_below="@id/tv_desc" android:layout_marginTop="3dp" android:background="#7000"></View></RelativeLayout>

希望在使用該組件的地方,能通過如此配置使用. 命名空間,以及3個自定義是屬性

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:yhb="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.yhb.mobilesafe.activity.SettingActivity"> <TextView style="@style/TitleStyle" android:text="設(shè)置中心" /> <com.example.yhb.mobilesafe.view.SettingItemView android:id="@+id/siv_update" android:layout_width="match_parent" android:layout_height="wrap_content" yhb:desc_off="自動更新已關(guān)閉" yhb:desc_on="自動更新已開啟" yhb:setting_item_title="自動更新設(shè)置"></com.example.yhb.mobilesafe.view.SettingItemView></LinearLayout>

  • 創(chuàng)建控件類

    通過分析布局文件,可得知該布局文件需要裝載在ViewGroup中,所以繼承ViewGroup的子類,通過View.inflate將布局文件填充進(jìn)來,同時可以獲取配置的屬性,并且暴露一些對外的公開方法

public class SettingItemView extends RelativeLayout {public final static String NAMESPACE = "http://schemas.android.com/apk/res-auto"; private TextView tvDesc; private TextView tvTitle; private CheckBox cbState; private String settingItemTitle; private String descOff; private String descOn; public SettingItemView(Context context) {super(context); initView(); }public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs); settingItemTitle = attrs.getAttributeValue(NAMESPACE, "setting_item_title"); //根據(jù)屬性名稱,獲取屬性的值 descOn = attrs.getAttributeValue(NAMESPACE, "desc_on"); //根據(jù)屬性名稱,獲取屬性的值 descOff = attrs.getAttributeValue(NAMESPACE, "desc_off"); //根據(jù)屬性名稱,獲取屬性的值 initView(); }public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr); initView(); }/** * 初始化布局 */ private void initView() {//arguments[2]: 將布局文件塞進(jìn)RelativeLayout或者解釋為 將RelativeLayout作為布局文件的父容器 View.inflate(getContext(), R.layout.view_setting_item, this); tvTitle = (TextView) findViewById(R.id.tv_tltle); tvDesc = (TextView) findViewById(R.id.tv_desc); cbState = (CheckBox) findViewById(R.id.cb_state); this.setTitle(settingItemTitle); }/** * 設(shè)置標(biāo)題 * * @param title */ public void setTitle(String title) {tvTitle.setText(title); }/** * 設(shè)置描述文件 * * @param desc */ public void setDesc(String desc) {tvDesc.setText(desc); }public void setChecked(boolean check) {if (check) {this.setDesc(descOn); } else {this.setDesc(descOff); }cbState.setChecked(check); }/** * 判斷復(fù)選框的勾選狀態(tài) * * @return */ public boolean isCheck() {return cbState.isChecked(); }}

  • 使用
public class SettingActivity extends AppCompatActivity {private SettingItemView sivUpdate; //設(shè)置自動更新 private SharedPreferences mPref; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); mPref = getSharedPreferences("config", MODE_PRIVATE); sivUpdate = (SettingItemView) findViewById(R.id.siv_update); boolean isAutoUpdate = mPref.getBoolean("auto_update", true); sivUpdate.setChecked(isAutoUpdate); sivUpdate.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {mPref.edit().putBoolean("auto_update", !sivUpdate.isCheck()).commit(); sivUpdate.setChecked(!sivUpdate.isCheck()); }}); }}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容