从TextViews / Layouts设置视图?

时间:2014-05-30 21:34:51

标签: android android-layout textview android-view

是否可以通过从已创建的布局中获取数据来设置视图的视图?

例如:

我有布局文件 test_layout.XML

<TextView
    android:id="@+id/TextView1"
    android:text="TextView1"
    android:background="@color/wisteria"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>   

我现在想要创建一个使用此TextView创建的新视图。 如果用Drawable创建一个新视图,我只会这样做:

imageView.setImageResource(R.drawable.shape1);

如何通过在TextView1中包含所有属性来创建新视图?我只是想从现有的TextView创建一个新视图。上面的TextView充当“模板”。我应该通过 setViewById 吗?

我希望自己清楚,有点难以形容。

1 个答案:

答案 0 :(得分:0)

只需使用LayoutInflater并再次充气此资源即可。

LayoutInflater inflater = LayoutInflater.from(context);
View v1 = inflater.inflate(R.layout.test_layout, null);
View v2 = inflater.inflate(R.layout.test_layout, null);
...