Set property custom Item

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="Please select a category"
            />

        <TextView
            android:id="@+id/tv_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="airplane"
            android:drawableRight="@drawable/arrow_right_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
</LinearLayout>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326987500&siteId=291194637