傅启平,神奇动物在哪里字幕,呼啦吧
一、定义一个xml布局文件
setting_item_view.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="60dip" > <textview android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_marginleft="5dip" android:layout_margintop="5dip" android:textcolor="#000000" android:textsize="20dip" /> <textview android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_title" android:layout_marginleft="5dip" android:layout_marginbottom="5dip" android:textcolor="#99000000" android:textsize="18dip" /> <checkbox android:clickable="false" android:focusable="false" android:id="@+id/cb_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="20dip" /> <view android:layout_width="fill_parent" android:layout_height="0.2dip" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_marginleft="5dip" android:layout_marginright="5dip" android:background="#000000" /> </relativelayout>
二、在src/values/attrs.xml中定义属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="textview"> <attr name="title" format="string" /> <attr name="desc_on" format="string" /> <attr name="desc_off" format="string" /> </declare-styleable> </resources>
三、自定义一个view继承自你需要的布局
iniview(context context)初始化自定义的布局文件
根据需求自定义一些api方法
public class settingitemview extends relativelayout { private checkbox cb_status; private textview tv_title; private textview tv_desc; private string title; private string desc_on; private string desc_off; public void iniview(context context){ view.inflate(context, r.layout.setting_item_view, this); cb_status = (checkbox)findviewbyid(r.id.cb_status); tv_title = (textview)findviewbyid(r.id.tv_title); tv_desc = (textview)findviewbyid(r.id.tv_desc); } public settingitemview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); iniview(context); } public settingitemview(context context, attributeset attrs) { super(context, attrs); iniview(context); title = attrs.getattributevalue("http://schemas.android.com/apk/res/com.victor.mobilesafe","title"); desc_on = attrs.getattributevalue("http://schemas.android.com/apk/res/com.victor.mobilesafe","desc_on"); desc_off = attrs.getattributevalue("http://schemas.android.com/apk/res/com.victor.mobilesafe","desc_off"); tv_title.settext(title); setdesc(desc_off); } public settingitemview(context context) { super(context); iniview(context); } public boolean ischecked(){ return cb_status.ischecked(); } public void setchecked(boolean checked){ if (checked) { setdesc(desc_on); }else{ setdesc(desc_off); } cb_status.setchecked(checked); } public void setdesc(string text){ tv_desc.settext(text); } }
四、在布局文件中使用该自定义组合控件
别忘记声明自定义命名空间
xmlns:victor="http://schemas.android.com/apk/res/com.victor.mobilesafe"
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:victor="http://schemas.android.com/apk/res/com.victor.mobilesafe" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.victor.mobilesafe.ui.settingitemview android:id="@+id/siv_update" android:layout_width="wrap_content" android:layout_height="wrap_content" victor:desc_off="自动更新关闭" victor:desc_on="自动更新开启" victor:title="设置自动更新" > </com.victor.mobilesafe.ui.settingitemview> </linearlayout>
总结:
1.自定义一个view 一般来说,继承相对布局,或者线性布局 viewgroup;
2.实现父类的构造方法。一般来说,需要在构造方法里初始化自定义的布局文件;
3.根据一些需要或者需求,定义一些api方法;
4.根据需要,自定义控件的属性,可以参照textview属性;
5.自定义命名空间,例如:
xmlns:victor="http://schemas.android.com/apk/res/<包名>" xmlns:victor="http://schemas.android.com/apk/res/com.victor.mobilesafe"
6.自定义我们的属性,在res/values/attrs.xml
7.使用我们自定义的属性
例如:
itheima:title="设置自动更新" itheima:desc_on="设置自动更新已经开启" itheima:desc_off="设置自动更新已经关闭"
8.在我们自定义控件的带有两个参数的构造方法里attributeset attrs 取出我们的属性值,关联自定义布局文件对应的控件。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论