android - 无法使用数据绑定(bind)在包含的布局中传递颜色变量

标签 android android-layout data-binding

我想将颜色变量传递给包含的布局,目前,我正在尝试从我的 colors.xml 传递颜色,并且我添加了变量类型 'android.graphics.Color'但它不起作用,Color 的变量类型应该是什么?

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

        <import type="android.view.View" />

        <variable
            name="isLoading"
            type="Boolean" />
        <variable
            name="loaderColor"
            type="android.graphics.Color" />
    </data>

    <com.wang.avi.AVLoadingIndicatorView
        android:layout_width="@dimen/btn_loading_indicator"
        android:layout_height="@dimen/btn_loading_indicator"
        android:layout_gravity="center"
        android:visibility="@{isLoading?View.VISIBLE:View.GONE}"
        app:indicatorColor="@{loaderColor}"
        app:indicatorName="LineSpinFadeLoaderIndicator" />
</layout>

主要布局

<include
                    layout="@layout/layout_btn_loading_indicator"
                    app:loaderColor="@{@color/theme_color}"
                    app:isLoading="@{viewModel.isLoading}" />

最佳答案

@color/color_name 是一个资源 Id,因此类型是 int。你的变量应该是这样的

<variable name="loaderColor" type="int" />

关于android - 无法使用数据绑定(bind)在包含的布局中传递颜色变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51288335/

相关文章:

java - setText 和 findViewById 不在 OnCreate 中导致应用程序崩溃

android - 无论如何要在 GridView Android 中设置行数

android - 使用 Fragment 更改 Android 布局

java - 我无法在表格 View 内设置按钮宽度

c# - 将文本绑定(bind)到组合框中的选定项目

c# - 如何将属性绑定(bind)到 xaml 中的属性值

Android:LinearLayout 的布局问题

android - 更改首选项 Activity 的外观

android - 针对 NexusOne/Nexus 为 Nexus 4/Nexus 7 提供布局资源

image - 将图像绑定(bind)到 URL Xamarin 表单 XAML