自定义RelativeLayout控件

在工作中,一般使用RelativeLayout相对布局较多,其通过自定义RelativeLayout控件的方式,可以提高代码的复用,减少不必要的代码和后期开发及维护的方便性:
其步骤为:
1,自定义相对布局::自定义一个类继承RelativeLayout,并将其所有构造函数初始化.

public class MyRelativeLayout  *extends RelativeLayout* {

    private TextView mTv_set_desc;
    private ImageView iv_set_update;
    private RelativeLayout rl_root;

    //用来创建对象
    public MyRelativeLayout(Context context) {
        super(context,null);
    }

    //用来实现具体的操作
    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initData(attrs);
    }

    private void initData(AttributeSet attrs) {

        *String desc = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "desc");
        String selectid = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "selectid");*
        switch (Integer.parseInt(selectid)){
            case 0:
                rl_root.setBackgroundResource(R.drawable.item_set_first_selector);
                break;
            case 1:
                rl_root.setBackgroundResource(R.drawable.item_set_middle_selector);

                break;
            case 2:
                rl_root.setBackgroundResource(R.drawable.item_set_last_selector);
                break;
        }
        mTv_set_desc.setText(desc);
    }

    //这个方法是给?谁能够获得这个MyRelativeLayout这个对象,谁就可以调
    public void setImageViewState(boolean state){
        if(state) {
            //说明自动更新图片,保存当前的状态
            iv_set_update.setImageResource(R.drawable.on);
        }else {
            //说明自动更新图片
            iv_set_update.setImageResource(R.drawable.off);
        }
    }

    private void initView(Context context) {
        rl_root = *(RelativeLayout)findViewById(R.id.rl_root);
        View view=View.inflate(context, R.layout.item_set_view,this);*
        mTv_set_desc = (TextView) findViewById(R.id.tv_set_desc);
        iv_set_update = (ImageView)findViewById(R.id.iv_set_update);

    }
}

2,自定义相对布局引入的XML布局:(R.layout.item_set_view)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    *android:id="@+id/rl_root"*
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/tv_set_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="16sp"/>

    <ImageView
        android:id="@+id/iv_set_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/off"/>

</RelativeLayout>

3,在原有布局的基础上,引入相对布局(引入自定义控件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              *xmlns:gaga="http://schemas.android.com/apk/res-auto"*
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:text="设置中心"
       style="@style/Tv_Title"/>
     *<com.m520it.mobilsafe.view.MyRelativeLayout
         android:id="@+id/myrl_update"
         gaga:selectid="first"
         android:layout_marginTop="20dp"
         gaga:desc="开启自动更新"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         ></com.m520it.mobilsafe.view.MyRelativeLayout>*

    <com.m520it.mobilsafe.view.MyRelativeLayout
        android:id="@+id/myrl_interface"
        android:layout_width="match_parent"
        gaga:selectid="last"
        gaga:desc="开启黑名监听"
        android:layout_height="wrap_content"
        ></com.m520it.mobilsafe.view.MyRelativeLayout>

    <com.m520it.mobilsafe.view.MyRelativeLayout
        android:id="@+id/myrl_show_address"
        android:layout_width="match_parent"
        gaga:selectid="first"
        android:layout_marginTop="20dp"
        gaga:desc="开启号码归属地显示"
        android:layout_height="wrap_content"
        ></com.m520it.mobilsafe.view.MyRelativeLayout>

    <com.m520it.mobilsafe.view.MyRelativeLayout
        android:id="@+id/myrl_watch"
        android:layout_width="match_parent"
        gaga:desc="开启看门狗"
        gaga:selectid="last"
        android:layout_height="wrap_content"
        ></com.m520it.mobilsafe.view.MyRelativeLayout>

</LinearLayout>

4,完成自定义的属性(位置在res/values/attrs.xml),这是必不可少,自定义控件需要根据自定义的属性来找到相应的资源:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyRelativeLayout">
        <attr name="desc" format="string" />
        <attr name="selectid">
            <enum name="first" value="0" />
            <enum name="middle" value="1" />
            <enum name="last" value="2" />
        </attr>
    </declare-styleable> 
</resources>

附带:三个选择器的代码:(位于res/drawable)

最上层:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/first_normal" android:state_pressed="false"></item>

    <item android:drawable="@drawable/first_pressed" android:state_pressed="true"></item>
</selector>

中层:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/middle_normal" android:state_pressed="false"></item>

    <item android:drawable="@drawable/middle_pressed" android:state_pressed="true"></item>
</selector>

下层:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/last_normal" android:state_pressed="false"></item>

    <item android:drawable="@drawable/last_pressed" android:state_pressed="true"></item>
</selector>

最后:需注意:自定义的 xmlns:gaga=”http://schemas.android.com/apk/res-auto”,其结构可以为:直接使用系统默认生成的,或者xmlns:你自己定义的名称=”http://schemas.android.com/apk/res/你程序的package包名”
最后:实现的效果为:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值