Android 将视图属性应用于其他视图

Android 将视图属性应用于其他视图,android,Android,是否可以将通过xml定义的一个视图的属性应用于以编程方式创建的另一个视图 在我的情况下,情况是下一个- 有一个自定义视图表单字段: class FormField extends LinearLayout { ... @Override protected void onFinishInflate() { super.onFinishInflate(); final int childCount = getChildCount();

是否可以将通过xml定义的一个视图的属性应用于以编程方式创建的另一个视图

在我的情况下,情况是下一个-

有一个自定义视图表单字段:

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传输到以onfinish方法动态创建的TextView。

FormField
的构造函数中,您获得视图的属性,然后您可以稍后将它们应用到
TextView
@Luksprog,我尝试过它,它适用于布局_margin*参数,但不适用于文本,不确定样式。如何尝试获取文本属性
context.ActainStyledAttributes()
应该允许您获取文本。@Luksprog我试图保存传递给TextField构造函数的属性集,然后使用TextView(context,AttributeSet)构造函数构造TextView。看起来某个地方的参数被过滤了,但我在Android源代码中找不到确切的位置。