在我的自定义视图上重用标准的android属性

Cod*_*ice 12 android android-custom-view android-view

我正在使用以下布局创建自定义复合视图

<merge xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="horizontal">

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:singleLine="true"/>

</merge>
Run Code Online (Sandbox Code Playgroud)

如你所见,它只是一个TextView和一个EditText.我希望能够给正在上要么转发我的自定义视图提供的属性TextViewEditText.例如

<codeguru.labelededittext.LabeledEditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:label="@string/label"
    app:hint="@string/hint"/>
Run Code Online (Sandbox Code Playgroud)

我已经想出如何将这些字符串属性转发给TextViewand EditText,并且具有代表性:

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LabeledEditText,
            0, 0);

    try {
        label.setText(a.getString(R.styleable.LabeledEditText_label));
        edit.setHint(a.getString(R.styleable.LabeledEditText_hint));
    } finally {
        a.recycle();
    }
Run Code Online (Sandbox Code Playgroud)

现在我也想设置inputTypeEditText.如果我创建一个<attr name="inputType" format="flag">标签,我是否必须用所有可能的标志值填充它?有没有办法重用已经声明的值EditText

Bar*_*ski 3

您可以通过以下方式获得:

int[] values = new int[]{android.R.attr.inputType};
TypedArray standardAttrArray = getContext().obtainStyledAttributes(attrs, values);
try {
    mInputType = standardAttrArray.getInt(0, EditorInfo.TYPE_NULL);
} finally {
    standardAttrArray.recycle();
}
Run Code Online (Sandbox Code Playgroud)