一。界面中子控件(对话框)使用自定义控件:
1.首先是自定义控件的布局,layout_diyControl.xml,
2.然后在使用这个控件的界面Activity.java里Xml转View:
1 2 3 4 |
// 上下文-因为这个对话框仅在此界面使用所以为this,源,被替换者 View alertDialogView = View.inflate(本界面Activity.this, R.layout.layout_diyControl, null); |
3.接着在Activity对应的xml布局文件里使用该控件,
1 2 3 4 5 6 7 |
// 对话框 AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(本界面Activity.this); alertDialog.create(); // 创建 alertDialog.setView(alertDialogView, 0, 0, 0, 0); // 这里为自定义组合控件 alertDialog.show(); // 显示 |
二。一般步骤:
1.首先是自定义控件的布局,layout_diy_relative.xml,
1 2 3 4 5 6 7 8 |
<RelativeLayout> // 这里按照一般情况设置text、size、color、等... <TextView android:id="@+id/tv_diy_title"/> <TextView android:id="@+id/tv_diy_info" /> <CheckBox android:id="@+id/cb_diy_power"/> </RelativeLayout> |
2.然后实现这个自定义控件,DiyRelativeLayout.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class DiyRelativeLayout extends RelativeLayout { private TextView textview1, textview2; private CheckBox checkbox; // 因为这个类将在界面的xml布局文件里调用,所以在这有2个参数的方法中转 public SettingItemRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); // xml转View(传入的上下文,自定义布局,替换本类) View.inflate(context, R.layout.layout_diy_relative, DiyRelativeLayout.this); // 实例化自定义控件的子控件 textview1 = (TextView) findViewById(R.id.tv_diy_title); textview2 = (TextView) findViewById(R.id.tv_diy_info); checkbox = (CheckBox) findViewById(R.id.cb_diy_power); } // 下面增强方法 } |
3.在DiyRelativeLayout.java里增加一些控制子控件的功能:
1 2 3 4 5 6 7 8 9 10 11 |
public void setChecked(boolean checked) { if(checked){ textview1.setText("TV1开"); textview2.setText("TV1开的很好"); } else { textview1.setText("TV1关"); textview2.setText("TV1关的很好"); } } |
4.在界面Activity的布局文件xml里引用此自定义类:
1 2 3 4 5 6 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout> <com.cuiweiyou.DiyRelativeLayout android:id="@+id/drl_ctrl" /> </LinearLayout> |
5.最后在Activity的.java中findViewById自定义控件,进行控制。
1 2 3 4 5 6 7 8 9 10 |
public class Activity extends Activity{ public void onCreate(Bundle savedInstanceState) { // 通过布局文件获得控件,也就得到了控件的实现类 DiyRelativeLayout drl_ctrl = (DiyRelativeLayout) findViewById(R.id.drl_ctrl); // 调用实现类里的方法 drl_ctrl.setCheckec(true); } } |
三。较复杂步骤:
1.创建/res/values/attrs.xml文件:
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TextView"> <!-- 这里是自定义属性 --> <attr name="str_diy_title" format="string" /> <attr name="str_diy_on" format="string" /> <attr name="str_diy_off" format="string" /> </declare-styleable> </resources> |
2.创建自定义组合控件的布局diy_rl.xml:
1 2 3 4 5 6 7 8 |
<RelativeLayout> // 这里TextView没有设置text,以为我们使用attrs.xml里的数据进行动态控制 <TextView android:id="@+id/tv_diy_title"/> <TextView android:id="@+id/tv_diy_info" /> <CheckBox android:id="@+id/cb_diy_power"/> </RelativeLayout> |
3.自定义组合控件的实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
public class DiyRelativeLayout extends RelativeLayout { private TextView tv_diy_title; private TextView tv_diy_info; private CheckBox cb_diy_power; private String str_diy_title; private String str_diy_on; private String str_diy_off; public SettingItemRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initItem(context); } /** 在xml布局里调用此类,调用此2个参数的构造方法 */ public SettingItemRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); // attrs.getAttributeValue在自定义约束中查找与attrs.xml里对应的自定义属性 str_diy_title = attrs.getAttributeValue("http://schemas.android.com/apk/res/cuiweiyou", "str_diy_title"); str_diy_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/cuiweiyou", "str_diy_on"); str_diy_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/cuiweiyou", "str_diy_off"); initItem(context); } /** 自身调用的增强功能 */ private void initItem(Context context) { /* (上下文,xml源,将生成的控件顶替this) */ View.inflate(context, R.layout.diy_rl, DiyRelativeLayout.this); tv_diy_title= (TextView) findViewById(R.id.tv_setting_item_autoupdate); tv_diy_title.setText(autoupdate_title); //---------------------------------//当前即设置,无需动态控制 tv_diy_info = (TextView) findViewById(R.id.tv_setting_item_autoupdate_info);//需动态控制 cb_diy_power = (CheckBox) findViewById(R.id.cb_setting_item_autoupdate); //控制器 } /** 增强功能-2<br/> 检测复选框是否勾选 */ public boolean isChecked(){ // 根据复选框的属性 return cb_diy_power.isChecked(); } /** 增强功能-2<br/> 检测复选框是否勾选 */ public void setChecked(boolean checked){ // 调用复选框的方法 cb_diy_power.setChecked(checked); // 同时动态控制其它控件 if(checked){ // 调用自定义约束里与attrs.xml里匹配的自定义属性值 tv_setting_item_autoupdate_info.setText(autoupdate_desc_on); }else{ tv_setting_item_autoupdate_info.setText(autoupdate_desc_off); } } /** new处理的对象,调用此1个参数的构造方法 */ public SettingItemRelativeLayout(Context context) { super(context); initItem(context); } } |
4.在界面的布局xml文件里使用自定义控件,同时配置自定义属性:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!-- 添加了一个自定义schema,cuiweiyou,同时指定自定义包名 --> <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" xmlns:cuiweiyou="http://schemas.android.com/apk/res/cuiweiyou"> <!-- 引用自定义组合控件实现类,配置自定义属性,属性名须和attrs.xml中的一致 --> <com.cuiweiyou.DiyRelativeLayout cuiweiyou:str_diy_title="自动更新" cuiweiyou:str_diy_on="自动更新功能已经打开" cuiweiyou:str_diy_off="自动更新功能已经关闭" android:id="@+id/drl_ctrl"/> </LinearLayout> |
5.在界面Activity中获取自定义控件:
1 2 3 4 5 |
DiyRelativeLayout drl_ctrl = (DiyRelativeLayout) findViewById(R.id.drl_ctrl); // 通过自定义控件的增强功能,控制其子控件 drl_ctrl.setChecked(true); |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/628.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设