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

Mat*_*att 40 android android-widget android-layout

我有一个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>
Run Code Online (Sandbox Code Playgroud)

及其包含的布局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>
Run Code Online (Sandbox Code Playgroud)

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

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

aro*_*ero 16

不,除了使用<include>指令的布局参数之外,没有办法将参数传递给包含的布局.

您可以以编程方式对布局进行充气并将其添加到视图中.在主布局中向容器添加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" />
Run Code Online (Sandbox Code Playgroud)

然后在你的活动中:

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
}
Run Code Online (Sandbox Code Playgroud)


the*_*kam 7

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

复用_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
       <variable name="text" type="String"/>
    </data>
    <LinearLayout 
        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="@{text}" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

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

<layout="@layout/reuse_layout" app:text="some tex" />
Run Code Online (Sandbox Code Playgroud)

  • 在布局“reuse_layout.xml”中,您可以定义变量。当您尝试引用数据时,请使用前缀“app:”,然后后跟变量的名称。我猜你尝试使用“@string”发送字符串,对吧?在这种情况下,请使用 `app:text="@{@string/your_string_key}"` (2认同)

Joh*_*ker 5

https://developer.android.com/training/improving-layouts/reusing-layouts.html

您可以设置每个include的android:id属性.这给了包含布局的根元素的id.

通过id获取该视图,然后找到要更改文本的子视图.