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

发布于 2024-12-28 19:07:52 字数 1453 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想念有你 2025-01-04 19:07:52

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

您可以通过编程方式扩充布局并将它们添加到您的视图中。将 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" />

然后在您的 Activity 中:

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
}

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:

<?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" />

Then in your Activity:

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
}
安穩 2025-01-04 19:07:52

如果您启用数据绑定,这是可能的:

reuse_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>

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

<layout="@layout/reuse_layout" app:text="some tex" />

It is possible if you enable data binding:

In reuse_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>

when you call the reuse_layout.xml just add app:text attribute with it

<layout="@layout/reuse_layout" app:text="some tex" />
冷了相思 2025-01-04 19:07:52

https://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.

不美如何 2025-01-04 19:07:52

我认为我找到了一种简单的方法,但如果用例很高,它可能会创建大量样板代码。但它对我来说工作得很好

第一步:

将数据绑定元素添加到应用程序模块中的build.gradle文件

android{
...
dataBinding { enabled = true }
...
}

第二步:

为可重用布局创建一个布局文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="text"
            type="String" />
    </data>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/card"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/sortBtnBG"
        app:contentPadding="5dp"
        app:strokeColor="@color/cardStrokeColor"
        app:strokeWidth="1dp">

        <TextView
            android:id="@+id/card_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@{text}"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </com.google.android.material.card.MaterialCardView>



</layout>

添加id对于您需要更改的元素,在我的例子中,我正在更改文本

第三步:

使用在活动布局中使用您的布局,

 <LinearLayout
        android:id="@+id/sorting_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp">

         <include layout="@layout/your_layout_name"
          android:id="@+id/customCard1"/>
     

          <include layout="@layout/your_layout_name"
          android:id="@+id/customCard2"/>

</LinearLayout>

确保你给的id

最后一步:

在您的 Activity.java 文件中添加以下内容:

View custom_card1 = findViewById(R.id.customCard1);
TextView cc1 = view.findViewById(R.id.card_text);
cc1.setText("hello");

View custom_card2 = findViewById(R.id.customCard2);
TextView cc2 = view.findViewById(R.id.card_text);
cc2.setText("word");

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

android{
...
dataBinding { enabled = true }
...
}

Second Step:

Create a layout file for your reusable layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="text"
            type="String" />
    </data>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/card"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/sortBtnBG"
        app:contentPadding="5dp"
        app:strokeColor="@color/cardStrokeColor"
        app:strokeWidth="1dp">

        <TextView
            android:id="@+id/card_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@{text}"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </com.google.android.material.card.MaterialCardView>



</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/>

 <LinearLayout
        android:id="@+id/sorting_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp">

         <include layout="@layout/your_layout_name"
          android:id="@+id/customCard1"/>
     

          <include layout="@layout/your_layout_name"
          android:id="@+id/customCard2"/>

</LinearLayout>

make sure your give id to the <inlcude/>

Final Step:

In your activity.java file add this:

View custom_card1 = findViewById(R.id.customCard1);
TextView cc1 = view.findViewById(R.id.card_text);
cc1.setText("hello");

View custom_card2 = findViewById(R.id.customCard2);
TextView cc2 = view.findViewById(R.id.card_text);
cc2.setText("word");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文