标签:
1、创建一个你喜欢的view_setting_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dp"
android:paddingLeft="8dp"
android:paddingTop="8dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动更新设置"
android:textColor="@color/black"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_desc"
android:layout_marginTop="3dp"
android:layout_below="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动更新已设置"
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"/>
<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:background="#a000"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
2、自定义RelativeLayout
View.inflate(getContext(), R.layout.view_setting_item,this);
参数1:Context
参数2:resId 资源ID
参数3:ViewGroup 视图组,也就是资源ID的父亲
package view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import com.example.mobile.R;
public class SettingItemView extends RelativeLayout{
//每次创建一个layout时,一定会先new一个对象
//不管使用那个构造函数,都为它生成一个自定义成员
public SettingItemView(Context context) {
super(context);
initView();
}
//有属性时调用
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
//有属性、样式时调用
public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView(){
//将自定义的布局文件设置给当前的SettingItemView
//也就是说:它一出生就有一个view_setting_item布局
View view=View.inflate(getContext(), R.layout.view_setting_item,this);
}
}
3、自定义布局的使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
style="@style/TitleStyle"
android:text="设置中心"/>
<view.SettingItemView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</view.SettingItemView>
</LinearLayout>
标签:
原文地址:http://www.cnblogs.com/324sige/p/5729690.html