我想根据我在dimens.xml中创建的维度设置边距它自己的尺寸工作得很好,它只是数据绑定在下面的情况下找不到它:
android:id="@+id/title_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/disableButton"
*************
android:layout_marginBottom="@{@bool/showAds ?
@dimen/frontpage_margin_ads: @dimen/frontpage_margin_noads}"
*************
android:gravity="center_horizontal"
android:text="@string/app_name"
android:textColor="@android:color/holo_orange_dark"
android:contentDescription="@string/app_name"
android:textSize="64sp"
android:textStyle="bold" />
它确实找到了它,但它说marginbottom不能采用float类型.我怎样才能解决这个问题?我尝试将这两个维度转换为int,但后来却抱怨它无法转换为int.
我的维度xml文件如下所示:
16dp
16dp
44sp
44sp
40sp
24sp
16dp
0dp
13dp
解决方法:
这里的问题不是维度,而是android:layout_marginBottom.任何LayoutParams属性都没有内置支持.这样做是为了删除许多可能用于将变量绑定到LayoutParams的“脚枪”,并且可能尝试使用数据绑定以这种方式为其位置设置动画.
数据绑定非常适合在您的示例中使用,您可以轻松添加自己的数据绑定.这将是这样的.
@BindingAdapter("android:layout_marginBottom")
public static void setBottomMargin(View view, float bottomMargin) {
MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin,
layoutParams.rightMargin, Math.round(bottomMargin));
view.setLayoutParams(layoutParams);
}
当然,您也可以添加left,top,right,start和end BindingAdapters.
标签:android,data-binding
来源: https://codeday.me/bug/20191007/1869126.html