组合控件-自定义属性
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="我是文本"/>
</LinearLayout>
public class SettingItemView extends LinearLayout{
public SettingItemView(Context context) {
super(context);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.item_layout, this);
}
public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
<com.example.jy_2016.smarttablayoutdemo.SettingItemView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
- 自定义属性
- 在values文件夹中,创建一个arrest.xml文件
- declare-styleable name : 值一般为我们定义的类名
- name:属性名
- format:类型
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingItemView">
<attr name="title" format="string"/>
<attr name="content" format="string"/>
</declare-styleable>
</resources>
public class SettingItemView extends RelativeLayout{
public SettingItemView(Context context) {
super(context);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.item_layout, this);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
String titleStr = typedArray.getString(R.styleable.SettingItemView_title);
String contentStr = typedArray.getString(R.styleable.SettingItemView_content);
TextView title = (TextView) findViewById(R.id.title);
TextView content = (TextView) findViewById(R.id.content);
title.setText(titleStr);
content.setText(contentStr);
}
public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}