是否可以将通过xml定义的一个视图的属性应用于以编程方式创建的其他视图?
在我的情况下,下一步是 -
有自定义视图FormField:
class FormField extends LinearLayout {
...
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final int childCount = getChildCount();
if (childCount > 1) {
throw new IllegalStateException("FormField can store only one child view");
}
labelView = new TextView(getContext());
addView(labelView, 0);
}
使用下一个目标:
<com.korovyansk.android.views.FormField
style="@style/Label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Field label"
android:layout_marginBottom="300dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp">
<com.korovyansk.android.views.EditText
style="@style/TextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
android:imeOptions="actionNext"/>
</com.korovyansk.android.views.FormField>
所以我的想法是将属性(样式,文本,边距)从FormField传递到onFinishInflated方法中动态创建的TextView。