Android 设置包含布局的子元素的属性

Android 设置包含布局的子元素的属性,android,android-layout,android-widget,Android,Android Layout,Android Widget,我有一个main.xml文件,描述我的主要活动的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientatio

我有一个main.xml文件,描述我的主要活动的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include layout="@layout/mylayout" />
    <include layout="@layout/mylayout" />
    <include layout="@layout/mylayout" />
    <include layout="@layout/mylayout" />
    <include layout="@layout/mylayout" />
    <include layout="@layout/mylayout" />

</LinearLayout>

及其包含的布局xml文件(mylayout.xml):


我只想在我的主布局中包含“mylayout”5次,但我不想看到“hello world”5次,而是希望TextView包含自定义文本

有没有办法通过在include元素上设置一些属性来覆盖子TextView的文本来做到这一点?实现这一目标的最佳方法是什么?

您可以设置每个include的android:id属性。这将为包含的布局的根元素提供id


按id获取该视图,然后找到要在其上更改文本的子视图

否,除了使用
指令的布局参数外,无法将参数传递到包含的布局

可以通过编程方式放大布局,并将其添加到视图中。将id添加到主布局中的容器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" />

然后在您的活动中:

ViewGroup container = (ViewGroup)findViewById(R.id.container);
for (int i = 0; i < 6; i++) {
    View myLayout = getLayoutInflater.inflate(R.layout.mylayout, null);
    TextView tv = myLayout.findViewById(R.id.textView);
    tv.setText("my layout " + i);
    container.addView(myLayout); // you can pass extra layout params here too
}
ViewGroup container=(ViewGroup)findviewbyd(R.id.container);
对于(int i=0;i<6;i++){
查看myLayout=GetLayoutFlater.inflate(R.layout.myLayout,null);
TextView tv=myLayout.findViewById(R.id.TextView);
tv.setText(“我的布局”+i);
container.addView(myLayout);//您也可以在此处传递额外的布局参数
}

如果启用数据绑定,则有可能:

在reuse_layout.xml中


调用reuse_layout.xml时,只需添加
app:text
属性即可



此外,我正在接听电话,因此很难键入。我正在尝试在布局XML中而不是在代码中找到实现这一点的方法。这是不可能的吗?我也遇到了这样的问题,并且发现重复使用复杂的布局几次会花费太多的代码来填充属性。也许在布局xml中复制粘贴相同的结构更好。这似乎是一个严重的限制,应该加以纠正。我刚刚提交了一个。app:text是在哪里定义的,因为我在编译时遇到了这个错误:“error:(461)解析XML:unbound prefix时出错”,它抱怨app:text属性。谢谢在布局中,“reuse_layout.xml”是您可以定义变量的地方。尝试使用数据时,请使用前缀
app:
,然后后跟变量名称。我猜你是想用
@string
发送字符串,对吗?在这种情况下,请使用
app:text=“@{@string/your_string_key}”
谢谢,我想我遗漏了一些东西,因为即使上面的代码按原样复制,我也会遇到这个错误。我得到这个错误:“错误:(461)解析XML时出错:未绑定前缀”您需要
xmlns:app=”http://schemas.android.com/apk/res-auto"
在根元素(
布局
)中使用
应用程序:
前缀。然后,您需要将文本字符串放入数据绑定表达式中,以便提取它,例如,
app:text=“@{“some text”}”
。请参阅。不要忘记将dataBinding元素添加到应用程序模块android{…dataBinding{enabled=true}中的build.gradle文件中
ViewGroup container = (ViewGroup)findViewById(R.id.container);
for (int i = 0; i < 6; i++) {
    View myLayout = getLayoutInflater.inflate(R.layout.mylayout, null);
    TextView tv = myLayout.findViewById(R.id.textView);
    tv.setText("my layout " + i);
    container.addView(myLayout); // you can pass extra layout params here too
}