【问题标题】: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_width="match_parent"
    android:layout_height="wrap_content"
    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}"

标签: android kotlin android-layout layout android-include


【解决方案1】:

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

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2011-06-29
  • 2020-02-01
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 2015-12-16
相关资源
最近更新 更多