How do I apply mathematical operations to Android dimensions?

前端 未结 4 1319
-上瘾入骨i
-上瘾入骨i 2020-12-24 11:05

How do I avoid this hardcoded math...


 10dip
 6dip
 

        
4条回答
  •  情话喂你
    2020-12-24 11:30

    Using databinding:

    android:layout_marginTop="@{@dimen/uno + @dimen/dos}"
    

    IFAIK margins adapters are not provided by the sdk. You will need to define it yourself:

    @BindingAdapter("android:layout_marginTop")
    public static void setBottomMargin(View view, int bottomMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin,
                layoutParams.rightMargin, bottomMargin);
        view.setLayoutParams(layoutParams);
    }
    

    Make sure databinding is enabled for your project :

    dataBinding {
        enabled = true
    }
    

    in your build.gradle.

    The databinding doc is worth reading.

提交回复
热议问题