[英]How to mention attribute(?attr) inside ternary operator in android data binding?
這是布爾數據
<variable
name="isSelected"
type="boolean" />
這就是我嘗試根據選擇更改視圖背景顏色的方式,在我的情況下,我應該根據選擇更改文本視圖顏色、背景顏色和圖標顏色。
這是我嘗試過的,但我在 Android Studio 中遇到錯誤。
android:backgroundTint="@{isSelected ? ?attr/colorPrimaryVariant : ?attr/colorPrimary}"
這是一個錯誤
<expr> expected, got '?'
我不確定它是否仍然相關。 我遇到了同樣的問題並找到了解決方案。
R
類<import type="com.example.test.R" />
"@{isSelected ? R.attr.colorPrimaryVariant : R.attr.colorPrimary}"
BindingAdapter
因為沒有它會崩潰像這樣的東西:
@BindingAdapter("backgroundAttr")
fun setBackgroundAttr(view: View, @AttrRes color: Int) {
view.setBackgroundColor(MaterialColors.getColor(view, color)
}
app:backgroundAttr="@{isSelected ? R.attr.colorPrimaryVariant : R.attr.colorPrimary}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.