设置包含布局的子元素的属性
我有一个 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):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我只想在我的主布局中包含“mylayout”5 次,而不是看到“hello world” 5 次,我希望 TextView 包含自定义文本。
有没有办法通过在 include 元素上设置某些属性来覆盖子 TextView 的文本来做到这一点?实现这一目标的最佳方法是什么?
I have a main.xml file describing the layout of my main activity:
<?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>
and its included layout xml file (mylayout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I just want to include "mylayout" in my main layout 5 times, but instead of seeing "hello world" 5 times, I want the TextView to contain custom text.
Is there any way to do this by setting some attribute on the include element to override the child TextView's text? What would be the best approach to take to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,除了使用
指令的布局参数之外,无法将参数传递到包含的布局。您可以通过编程方式扩充布局并将它们添加到您的视图中。将 id 添加到主布局中的容器:
然后在您的 Activity 中:
No, there is no way to pass parameters to the included layout other than the layout params using the
<include>
directive.You can inflate the layout programatically and add them to your view. Add an id to the container in your main layout:
Then in your Activity:
如果您启用数据绑定,这是可能的:
在 reuse_layout.xml 中
,当您调用 reuse_layout.xml 时,只需添加
app:text
属性即可It is possible if you enable data binding:
In reuse_layout.xml
when you call the reuse_layout.xml just add
app:text
attribute with ithttps://developer.android.com/training/improving-layouts/reusing -layouts.html
您可以设置每个包含的 android:id 属性。这为所包含的布局的根元素提供了 id。
通过 id 获取该视图,然后找到要更改文本的子视图。
https://developer.android.com/training/improving-layouts/reusing-layouts.html
You can set the android:id property of each include. That gives the id to the root element of the layout being included.
Get that view by id then find the subview you want to change the text on.
我认为我找到了一种简单的方法,但如果用例很高,它可能会创建大量样板代码。但它对我来说工作得很好
第一步:
将数据绑定元素添加到应用程序模块中的build.gradle文件
第二步:
为可重用布局创建一个布局文件
添加id对于您需要更改的元素,在我的例子中,我正在更改文本
第三步:
使用
在活动布局中使用您的布局,确保你给的id
最后一步:
在您的 Activity.java 文件中添加以下内容:
I think I found an easy way, but it could create a lot of boilerplate code if the use case is high. but it's working perfectly for me
First Step:
Add data binding element to your build.gradle file in the app module
Second Step:
Create a layout file for your reusable layout
add id for the elements you need to change, in my case, I am changing the text
Third Step:
Use your layout in your activity layout using
<include/>
make sure your give id to the
<inlcude/>
Final Step:
In your activity.java file add this: