设置自定义控件view(自定义相对布局和对话框)

来源:互联网 发布:剑网三重置网络命令 编辑:程序博客网 时间:2024/05/16 19:35

自定义相对布局

1.先设置好要自定义成View的layout布局文件

<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/color_Text"        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>

2.新建attrs.xml,加入属性名和值类型,创建属性是为了暴露出一些接口给我们方便写值

<resources>    <declare-styleable name="SettingItemView">        <attr name="set_title" format="string" />        <attr name="desc_on" format="string" />        <attr name="desc_off" format="string" />    </declare-styleable></resources>

3.创建class并继承父类(这里父类可以是别的viewGroup)

public class SettingItemView extends RelativeLayout
4.继承父类的3个构造方法,自定义初始化方法覆盖父类构造方法,和从布局文件中获取定义的属性值
//命名空间,在调用此自定义View的布局文件中需要设置,必须为项目包名private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.example.wanghao.didisafe";
// 用代码new对象时,走此方法public SettingItemView(Context context) {    super(context);    initView();//初始化布局}// 有属性时走此方法public SettingItemView(Context context, AttributeSet attrs) {    super(context, attrs);    mtitle = attrs.getAttributeValue(NAMESPACE, "set_title");    mdesc_on = attrs.getAttributeValue(NAMESPACE, "desc_on");    mdesc_off = attrs.getAttributeValue(NAMESPACE, "desc_off");    Log.d("TAG",mtitle+"__"+mdesc_on+"_______"+mdesc_off);    initView();}// style样式的话会走此方法public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    initView();}private void initView() {    //将定义好的子项布局文件加载给自定义view,并传给当前的class实例    View.inflate(getContext(), R.layout.setting_item_view, this);    //这里获取子项实例不需要view.是应为传入的class的实例。    tv_title = (TextView) findViewById(R.id.tv_title);    tv_desc = (TextView) findViewById(R.id.tv_desc);    cb_status = (CheckBox) findViewById(R.id.cb_status);    setTitle(mtitle);//这里要特别注意,必须要我们手动在这里或者外部类条用方法,来把属性取的值填入自定义控件}
5.自定义方法,public的方法在拿到自定义view的实例后都是可以在外部类里调用的
private void setTitle(String mtitle) {    tv_title.setText(mtitle);}private void setDesc(String mdesc){    tv_desc.setText(mdesc);}public void setChecked(Boolean checked) {    cb_status.setChecked(checked);    if (checked){        setDesc(mdesc_on);    }else {        setDesc(mdesc_off);    }}public boolean isChecked() {    checked = cb_status.isChecked();    return checked;}
6.在别的布局文件中调用自定义view
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
这行是要我们自己添加的,wanghao是自己随意定义的,后面的则必须上项目包名
xmlns:wanghao="http://schemas.android.com/apk/res/com.example.wanghao.didisafe"
    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        style="@style/TV_Style_Title"        android:text="设置中心"/>    <com.example.wanghao.didisafe.view.SettingItemView        android:id="@+id/siv_update"        android:layout_width="match_parent"        android:layout_height="wrap_content"        wanghao:set_title="自动更新设置"        wanghao:desc_off= "自动更新已关闭"        wanghao:desc_on="自动更新已开启"        ></com.example.wanghao.didisafe.view.SettingItemView>
7.在外部类里调用自定义的方法
msiv_update = (SettingItemView) findViewById(R.id.siv_update);
mauto_update = mconfig.getBoolean("auto_update", true);//判断用户是否已经勾选if (mauto_update) {    msiv_update.setChecked(true);} else {    msiv_update.setChecked(false);}
自定义对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);final AlertDialog dialog = builder.create();View view = View.inflate(this, R.layout.input_password_dailog, null);// dialog.setView(view);// 将自定义的布局文件设置给dialogdialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题

0 0
原创粉丝点击