Android Data Binding layout_width and layout_height

前端 未结 6 673
情话喂你
情话喂你 2020-12-08 15:12

I need to be able to dynamically set an EditText\'s height property. I am using data binding for other properties throughout my app, so I would love to be able to use data b

相关标签:
6条回答
  • 2020-12-08 15:37

    When data binding is used, we strip values from the XML. You can add a default value to be used when it is stripped to avoid the issue.

    see: http://developer.android.com/tools/data-binding/guide.html (bottom of the page).

    android:layout_height="@{loginVM.compact ? @dimen/verificationHeightCompact : @dimen/verificationHeightFull, default=wrap_content}"
    
    0 讨论(0)
  • 2020-12-08 15:40

    In Java

    @BindingAdapter("layout_height")
    public static void setLayoutHeight(View view, float height) {
        LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }
    

    And in your XML

    app:layout_height="@{ viewModel.isBig ? @dimen/dp_20 : @dimen/dp_5 }"
    

    import the app like this

    xmlns:app="http://schemas.android.com/apk/res-auto"
    
    0 讨论(0)
  • 2020-12-08 15:51

    You can add a binding adapter for android:layout_height that takes in a float.

    For height:

    @BindingAdapter("android:layout_height")
        public static void setLayoutHeight(View view, float height) {
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = (int) height;
            view.setLayoutParams(layoutParams);
        }
    

    For width:

    @BindingAdapter("android:layout_width")
    public static void setLayoutWidth(View view, float height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = (int) height;
        view.setLayoutParams(layoutParams);
    }
    

    Usage:

    android:layout_width="@{model.isBig ? @dimen/big_dimen : @dimen/small_dimen, default=wrap_content}"
    
    android:layout_height="@{model.isBig ? @dimen/big_dimen : @dimen/small_dimen, default=wrap_content}"
    

    PS: Don't forget to set a default value

    0 讨论(0)
  • 2020-12-08 15:53

    I solved it like this in Kotlin instead of Java.

    Make a file called

    MyBindings.kt
    

    And put in:

    @BindingAdapter("bind:customHeight")
    fun setLayoutHeight(view: View, height: Float) {
        view.layoutParams = view.layoutParams.apply { this.height = height.toInt() }
    }
    

    And then you can use this in your layouts:

    <EditText 
    android:id="@+id/internalTopDisplay"
    android:layout_width="match_parent"
    bind:customHeight="@{loginVM.compact ? @dimen/verificationHeightCompact : @dimen/verificationHeightFull}"
    android:layout_height="wrap_content"/>
    
    0 讨论(0)
  • 2020-12-08 15:57

    For me did this the trick:

        @BindingAdapter("android:layout_height")
        public static void setLayoutHeight(View view, final Number height) {
            final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height =  height.intValue();
            view.setLayoutParams(layoutParams);
        }
    

    And the xml:

    android:layout_height="@{item.isItemCollapsed ? @dimen/radar_item_first_background_height  : item.textMaxLines, default=@dimen/radar_item_first_background_height}"
    
    0 讨论(0)
  • 2020-12-08 16:02

    According to the discussion on Android issue tracker, it is impossible to set layout height or width with data binding without creating custom binding adapters:

    https://code.google.com/p/android/issues/detail?id=180666

    The binding adapter needed for setting view height would look like that:

    @BindingAdapter("android:layout_height")
    public static void setLayoutHeight(View view, int height) {
        LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }
    
    0 讨论(0)
提交回复
热议问题