【发布时间】:2020-11-18 14:19:57
【问题描述】:
我喜欢根据条件将边距设置为 TextView。我已经在维度文件中声明了需要根据条件设置的值。
<dimen name="tall">15dp</dimen>
<dimen name="short">5dp</dimen>
在视图 xml 中,我正在尝试设置边距
<TextView
android:id="@+id/personal_bio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="@{isTall ? @dimen/tall:@dimen/short}"
android:singleLine="true"
android:text="@{viewmodel.personalBio}"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
但是在编译时,这会抛出类似Cannot find a setter for <android.widget.TextView android:layout_marginTop> that accepts parameter type 'float'的错误
类似的条件适用于其他属性,如可见性、颜色和其他少数属性,但无法设置边距。
是否有任何解决方法可以在 xml 本身中实现这一点?
【问题讨论】:
-
在这种情况下,您可以为此进行自定义绑定,您可以在其中设置边距值动态
标签: android android-layout android-view