将属性从包含的布局传递给布局时出错

Posted

技术标签:

【中文标题】将属性从包含的布局传递给布局时出错【英文标题】:error when passing attribute to a layout from included layout 【发布时间】:2021-11-21 02:28:25 【问题描述】:

为了让我的应用程序代码更简洁,我尝试创建通用 xml,但出现如下错误:

error: activity_login.xml:230: AAPT: error: attribute buttonTitle

在我的 Login.xml 文件中:

 <include
        android:id="@+id/login_button"
        layout="@layout/widget_large_button"
        app:buttonTitle="@string/login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"/>

我已经定义了如下布局:

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

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/button"
    android:layout_
    android:layout_
    android:layout_marginStart="25dp"
    android:layout_marginEnd="25dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/stripe_backgound_05"
    android:clickable="false"
    android:focusable="false"
    android:letterSpacing="0.5"
    android:paddingTop="25dp"
    android:paddingBottom="25dp"
    android:text="@buttonTitle"
    android:textAllCaps="true"
    android:textColor="@color/gainsboro_05"
    android:textSize="16sp"/>

似乎我无法将 login.xml 中的 buttonTitle 传递给按钮小部件。

有什么想法吗?

谢谢

【问题讨论】:

你可以尝试将变量类型从 String 更改为 CharSequence。 它仍然无法正常工作。它不是来自类型。它真的来自 2 布局之间的链接 你能传递这样的字符串吗:app:buttonTitle="@@string/login" 【参考方案1】:

尝试在包含标签中添加bind:buttonTitle="@'variable name of login.xml'"

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于将属性从包含的布局传递给布局时出错的主要内容,如果未能解决你的问题,请参考以下文章

包含布局时如何解决 inflateException 问题?

Android 视图绑定:从包含的 xml 布局访问 NavHostFragment 时

css3弹性盒模型(Flexbox)

将二维字符串数组传递给函数时出错 (C++)

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

MVC 4如何将模型属性列表从视图传递给Action