Android数据绑定-如何从dimens.xml获取维度
我想根据我在dimens.xml中创建的维度设置边距。该维度本身工作正常,只是数据绑定在以下情况下找不到它:Android数据绑定-如何从dimens.xml获取维度,android,data-binding,Android,Data Binding,我想根据我在dimens.xml中创建的维度设置边距。该维度本身工作正常,只是数据绑定在以下情况下找不到它: <TextView android:id="@+id/title_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"
<TextView
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文件如下所示:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="bigText">44sp</dimen>
<dimen name="littleText">44sp</dimen>
<dimen name="mediumText">40sp</dimen>
<dimen name="smallText">24sp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="frontpage_margin_noads">0dp</dimen>
<dimen name="frontpage_margin_ads">13dp</dimen>
</resources>
16dp
16dp
44便士
44便士
40便士
24便士
16dp
0dp
13dp
这里的问题不在于尺寸,而在于android:layout\u 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);
}
当然,您还可以添加左、上、右、开始和结束BindingAdapters
。几乎相同的解决方案,但使用Kotlin:
在BindingAdapters.kt文件中添加:
@BindingAdapter("layoutMarginBottom")
fun setLayoutMarginBottom(view: View, dimen: Float) {
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = dimen.toInt()
}
}
@BindingAdapter(“layoutMarginBottom”)
fun setLayoutMarginBottom(视图:视图,尺寸:浮动){
view.updateLayoutParams{
bottomMargin=dimen.toInt()
}
}
布局中的用法:
<LinearLayout
app:layoutMarginBottom="@{viewModel.type == Type.SMALL ? @dimen/margin_small : @dimen/margin_large}"
其他示例对我不起作用,因此我编写了自己的示例。可以将其添加到新的空白Kotlin文件中。函数不需要在类中。确保在文件的顶部有自己的包语句
import android.view.View
import android.view.ViewGroup
import androidx.databinding.BindingAdapter
@BindingAdapter("android:layout_marginBottom")
fun setLayoutMarginBottom(view: View, dimen: Int) {
(view.layoutParams as ViewGroup.MarginLayoutParams).let {
it.bottomMargin = dimen
view.layoutParams = it
}
}
@BindingAdapter("android:layout_marginTop")
fun setLayoutMarginTop(view: View, dimen: Int) {
(view.layoutParams as ViewGroup.MarginLayoutParams).let {
it.topMargin = dimen
view.layoutParams = it
}
}
@BindingAdapter("android:layout_marginStart")
fun setLayoutMarginStart(view: View, dimen: Int) {
(view.layoutParams as ViewGroup.MarginLayoutParams).let {
it.marginStart = dimen
view.layoutParams = it
}
}
@BindingAdapter("android:layout_marginEnd")
fun setLayoutMarginEnd(view: View, dimen: Int) {
(view.layoutParams as ViewGroup.MarginLayoutParams).let {
it.marginEnd = dimen
view.layoutParams = it
}
}
我应该在哪里写这段代码?我指定的所有marginBottom布局都将使用此方法,还是仅使用数据绑定方法,或者仅使用此特定布局?您可以在启用数据绑定的项目中的任何(公共)类上添加此方法。它将用于所有数据绑定页边距属性。问题很简单:页边距不是视图的属性,而是视图的布局实例。您可以使用其他视图属性(如padding)执行您尝试的操作,但边距属性的前缀为layout\uu
@GeorgeMount有一个很好的原因,我发现在AS 2.2 preview 6中,我需要将bottomMargin的类型设置为float而不是int,否则我会得到编译错误,说在android.widget.TextViewNo上找不到参数类型为float的属性“android:layout_marginBottom”的setter,这不是错误@dimen
资源是浮点,而@dimenOffset
和@dimenSize
是整数。这分别对应于Resources.getDimension()、getDimensionPixelSize()和getDimensionPixelOffset()。使用浮点始终是安全的,因为整数将自动转换为浮点,但浮点不会自动转换为整数。