您的位置:首页 > 其它

自定义RelativeLayout控件

2016-11-17 20:04 190 查看
在工作中,一般使用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包名”

最后:实现的效果为:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息