[英]How to change background via data binding in Android layout xml?
I want to use the folloeing code to change the background of ConstraintLayout. 我想使用下面的代码来更改ConstraintLayout的背景。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View"/>
<import type="com.example.test.R" />
<variable
name="viewModel"
type="com.example.test.viewModel.MainViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:id="@+id/layoutOtpRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@{viewModel.isCheck() ? @android:color/pinkColor : @android:drawable/bg}">
</android.support.constraint.ConstraintLayout>
</layout>
But it is not working , the background did not show anything , I also try the folloeing...and it still not working 但是它没有用,背景什么也没显示,我也尝试了folloeing ...但它仍然没有用
android:background="@{viewModel.isCheck() ? color.pinkColor : R.drawable.bg}"
I am sure the viewModel.isCheck()
is working , but the background did not show anything. 我确定
viewModel.isCheck()
可以正常工作,但是背景什么也没显示。
Did I missing something ? 我错过了什么吗? Thanks in advance.
提前致谢。
大概吧 。
android:background="@{viewModel.isCheck() ? @color/pinkColor : @drawable/bg}"
I think you can add backgroud in the layout!
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android : background = "@color/colorPrinary" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.