How do I apply mathematical operations to Android dimensions?

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

How do I avoid this hardcoded math...


 10dip
 6dip
 

        
相关标签:
4条回答
  • 2020-12-24 11:14

    There's a few tricks around this, but it wouldn't be as nice as your proposed solution, which is something I want as well. For example, you can use layout paddings on not just the View (Button in this case), but you can also do it on the view's parent (the layout like LinearLayout/RelativeLayout). You can also put in invisibile Views (a straight View object works often) with fixed dimensions. It would be like

    <View
       android:layout_width="1px"
       andoird:layout_height="@dimen/dos" />
    

    Note that 1px is fine if want to guarantee only 1 pixel will be drawn for a dimension, which is usually what you want if you want use empty views for padding. Some say FrameLayout is better to use for empty padding, but that's descended from View

    Sometimes you can combine padding and the layout padding, but that can get messy and have your view cropped. Also you can have something like a FrameLayout or a LinearLayout contain just that view, and use that to have the added padding

    0 讨论(0)
  • 2020-12-24 11:20

    One trick for simple addition is to use margin + padding.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 11:33

    You don't, sorry. Layout XML files do not support expressions. You either:

    • Leave it as @dimen/uno_plus_dos, or
    • Set your margins in Java code, where you can replace a single resource with a bunch of extra lines of code, or
    • Write your own layout preprocessor that handles expressions like this

    UPDATE The data binding library supports some operations in its expressions. I am uncertain if it can handle this specific scenario.

    0 讨论(0)
提交回复
热议问题