gpt4 book ai didi

android - 使用 Android 绑定(bind)库将 dimen 资源添加到布局

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:10 26 4
gpt4 key购买 nike

我正在使用 Android 的绑定(bind)库,我正在尝试根据 bool 值添加或删除 TextView 上的边距。如果是这样,我希望 TextView 在右边有一个边距,在左边没有边距,如果没有,则相反。所有其他资源都工作正常,但是当我编译代码时,我收到有关 TextView 边距的错误:找不到参数类型为 float 的属性“android:layout_marginRight”的 setter 。

谁能发现错误?

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="comment" type="mx.com.corpogas.dataModels.FeedbackComment"/>
<import type="android.view.View"/>
</data>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@{comment.isMine ? @drawable/comment_background_white : @drawable/comment_background_green}"
android:textSize="15sp"
android:textColor="@{comment.isSent ? (comment.isMine ? @color/colorPrimaryDark : @android:color/white) : @color/unsent_text}"
android:layout_marginRight="@{comment.isMine ? @dimen/feedback_comment_margin : @dimen/feedback_comment_no_margin}"
android:layout_marginLeft="@{comment.isMine ? @dimen/feedback_comment_no_margin : @dimen/feedback_comment_margin}"
android:text="@{comment.content}"/>


</layout>

这是我的边距:

<dimen name="feedback_comment_margin">16dp</dimen>
<dimen name="feedback_comment_no_margin">0dp</dimen>

当我删除边距时,程序可以完美地编译和运行。

最佳答案

布局属性的数据绑定(bind)不受支持,但您可以在技术上自行添加它们。问题是这些很容易被试图制作动画的人滥用。要为您的应用程序实现这些,请创建一个绑定(bind)适配器:

@BindingAdapter("android:layout_width")
public static void setLayoutWidth(View view, int width) {
LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = width;
view.setLayoutParams(layoutParams);
}

关于android - 使用 Android 绑定(bind)库将 dimen 资源添加到布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34817486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com