public class Item extends RelativeLayout { private TextView tvTitle; private TextView tvValue; public Item(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.item); String title = a.getString(R.styleable.item_item_title); String value = a.getString(R.styleable.item_item_value); a.recycle(); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater .inflate(R.layout.widget_item, this, true); tvTitle = (TextView) view.findViewById(R.id.tv_title); tvValue = (TextView) view.findViewById(R.id.tv_value); tvTitle.setText(title); tvValue.setText(value); } public void setValue(String str) { if (TextUtils.isEmpty(str)) { tvValue.setText(""); } else { tvValue.setText(str); } } } <declare-styleable name="item"> <attr name="item_title" format="string"/> <attr name="item_value" format="string"/> </declare-styleable> <?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="@dimen/baby_add_item_heigh"> <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/baby_add_item_heigh" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="请选择分类" /> <TextView android:id="@+id/tv_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="飞机" android:drawableRight="@drawable/arrow_right_btn" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> </LinearLayout>
设置属性自定义Item
猜你喜欢
转载自u010991855.iteye.com/blog/2281206
今日推荐
周排行