75.s1-自定义组合控件--自定义属性(2)

    xiaoxiao2021-03-25  92

    把一些列的小的控件(view_setting_item.xml)组合成一个大的组合控件SettingItemView.java

    1.自定义一个Viw,继承ViewGroup,比如RelativeLayout

    2.编写组合控件的布局文件,在自定义的View中加载

               //将自定义好的布局文件设置给当前的SettingItemView

           View.inflate(getContext(),R.layout.view_setting_item, this);

    3.自定义属性attrs.xml

    小控件

    <?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="70dp" android:padding="5dp" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="22sp" /> <TextView android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_title" android:layout_marginTop="3dp" android:textColor="#a000" android:textSize="18sp" /> <CheckBox android:id="@+id/cb_status" 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_alignParentBottom="true" android:background="#a000" /> </RelativeLayout>

    使用方法activity_setting.xml

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_setting" style="@style/TitleStyle" android:text="设置中心" /> <com.ldw.mobilesafe.view.SettingItemView android:id="@+id/siv_update" android:layout_width="match_parent" android:layout_height="wrap_content"></com.ldw.mobilesafe.view.SettingItemView> </LinearLayout>

    大的组合控件SettingItemView.java

    package com.ldw.safe.view; import com.ldw.safe.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; /* * 设置自定义相对布局,布局的结构用view_setting_item进行填充,并初始化SettingItemView的方法,来设置或者获取某些参数 */ public class SettingItemView extends RelativeLayout { //NAMESPACE是自定义的最后的是包名 private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.ldw.safe"; private TextView tvTitle; private TextView tvDesc; private CheckBox cbStatus; private String mDescOff; private String mDescOn; private String mTitle; public SettingItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } public SettingItemView(Context context, AttributeSet attrs) { super(context, attrs); /*获取自定义属性的值 int attributeCount = attrs.getAttributeCount(); for(int i = 0; i< attributeCount; i++){ String attributeName = attrs.getAttributeName(i); String attributeValue = attrs.getAttributeValue(i); System.out.println(attributeName + "===" + attributeValue); } */ //根据属性的名称获取自定义属性的值,NAMESPACE是命名空间 mTitle = attrs.getAttributeValue(NAMESPACE, "title"); mDescOn = attrs.getAttributeValue(NAMESPACE, "desc_on"); mDescOff = attrs.getAttributeValue(NAMESPACE, "desc_off"); initView(); } public SettingItemView(Context context) { super(context); initView(); } //初始化布局 private void initView(){ //将定义的布局文件设置成为当前的SettingItemView View.inflate(getContext(), R.layout.view_setting_item, this); tvTitle = (TextView) findViewById(R.id.tv_title); tvDesc = (TextView) findViewById(R.id.tv_desc); cbStatus = (CheckBox) findViewById(R.id.cb_status); setTitle(mTitle);//设置标题 } //设置相对布局的元素的属性 public void setTitle(String title){ tvTitle.setText(title); } //设置相对布局的元素的属性 public void setDesc(String desc){ tvDesc.setText(desc); } //获取相对布局的元素的属性 public boolean isChecked(){ return cbStatus.isChecked(); } //设置相对布局的元素的属性 public void setChecked(boolean check){ cbStatus.setChecked(check); //根据选择的状态更新textView if(check){ setDesc(mDescOn); }else{ setDesc(mDescOff); } } }

    自定义组合控件添加属性/res/values/attrs.xml

    <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SettingItemView"> <attr name="title" format="string" /> <attr name="desc_on" format="string" /> <attr name="desc_off" format="string" /> </declare-styleable> </resources>

    使用自定义组合空间布局activity_setting.xml,需要添加上自定义组合空间的命名空间

    xmlns:ldw="http://schemas.android.com/apk/res/com.ldw.safe"最后是包名

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ldw="http://schemas.android.com/apk/res/com.ldw.safe" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView style="@style/TitleStyle" android:text="手机设置" /> <com.ldw.safe.view.SettingItemView android:id="@+id/siv_update" android:layout_width="match_parent" android:layout_height="wrap_content" ldw:title="自动更新设置" ldw:desc_on="自动更新已开启" ldw:desc_off="自动更新已关闭" /> </LinearLayout>

    使用自定义组合控件,不用在使用的过程中再设置属性

    ackage com.ldw.safe.Activity; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import com.ldw.safe.R; import com.ldw.safe.view.SettingItemView; /** * 设置中心 */ public class SettingActivity extends Activity { private SettingItemView siv_update;//设置自动更新 private SharedPreferences mPref;//把设置的数据保存在mPref @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); //把设置的数据保存在mPref mPref = getSharedPreferences("config", MODE_PRIVATE); siv_update = (SettingItemView)findViewById(R.id.siv_update); //siv_update.setTitle("自动更新设置"); //获取保存的数据,判断之前选择的是开始还是关闭,初始化进入界面是否勾选 boolean autoUpdate = mPref.getBoolean("auto_update", true); if(autoUpdate){ siv_update.setDesc("自动更新已经开启"); siv_update.setChecked(true); }else{ siv_update.setDesc("自动更新已经关闭"); siv_update.setChecked(false); } siv_update.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //判断右边框的勾选状态 if(siv_update.isChecked()){ //设置不勾选 siv_update.setChecked(false); //siv_update.setDesc("自动更新已经关闭"); //编辑mPref的值 mPref.edit().putBoolean("auto_update", false).commit(); }else{ //设置勾选 siv_update.setChecked(true); //siv_update.setDesc("自动更新已经开启"); //编辑mPref的值 mPref.edit().putBoolean("auto_update", true).commit(); } } }); } }

     

     

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-17737.html

    最新回复(0)