Android数据绑定layout_width和layout_height

时间:2016-02-09 14:54:46

标签: java android mvvm data-binding

我需要能够动态设置EditText的height属性。我在我的应用程序中使用数据绑定其他属性,所以我希望能够使用数据绑定来控制我的元素的高度。这是我的xml的精简版:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable name="loginVM" type="com.testing.stuff.ViewModels.LoginViewModel" />
</data>

<EditText android:inputType="number"
            android:id="@+id/txtVerificationCode"
            android:layout_height="@{loginVM.compact ? @dimen/verificationHeightCompact : @dimen/verificationHeightFull}"
            android:layout_width="match_parent"
            android:paddingRight="16dp"
            android:paddingLeft="16dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_marginLeft="10dp"
            android:alpha="@{loginVM.verificationOpacity}"
            android:layout_marginStart="10dp"
            android:textAlignment="center"
            android:visibility="visible"
            android:hint="Enter verificationCode"
            android:text="@{loginVM.verificationCode}" />
</layout> 

这是我的View模型的精简版:

public class LoginViewModel extends BaseObservable {
public final ObservableField<String> verificationCode; 
public final ObservableField<Boolean> compact;

@Bindable
public String getVerificationCode() {
    if (this.verificationCode == null) {
        return "";
    } else {
        return this.verificationCode.get();
    }
}

public void setVerificationCode(String verificationCode) {
    this.verificationCode.set(verificationCode);
    invalidateProperties();
}

@Bindable
public Boolean getCompact(){return this.compact.get();}

public void setCompact(Boolean value)
{
    this.compact.set(value);
    this.invalidateProperties();
}

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

public LoginViewModel(Context ctx) {
    verificationCode = new ObservableField();
    compact = new ObservableField();
}

维度位于dimens.xml文件中。我正在修改视图模型中的属性。但是,当我启动应用程序时,我在启动后立即收到以下错误(bindingadapter未在调试时触发)。我在屏幕上还有其他几个元素,但这个特殊的元素是我在特定动作发生时需要更改高度的元素:

FATAL EXCEPTION: main

Process: com.testing.stuff, PID: 32752

java.lang.RuntimeException: Unable to start activity  
ComponentInfo{com.testing.stuff/com.testing.stuff.Login}: 
java.lang.RuntimeException: Binary XML file line #69: You must supply 
a layout_height attribute.

Caused by: java.lang.RuntimeException: Binary XML file line #69: You 
must supply a layout_height attribute.

有关此问题的SO上有一些帖子,但没有明确的答案或方法不起作用。当然,这是一种常见的实施方式。在此先感谢您的帮助。

7 个答案:

答案 0 :(得分:31)

使用数据绑定时,我们从XML中删除值。您可以添加在剥离时使用的默认值,以避免出现此问题。

请参阅:http://developer.android.com/tools/data-binding/guide.html(页面底部)。

android:layout_height="@{loginVM.compact ? @dimen/verificationHeightCompact : @dimen/verificationHeightFull, default=wrap_content}"

答案 1 :(得分:28)

在Java中

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

并在您的XML中

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

像这样导入应用

xmlns:app="http://schemas.android.com/apk/res-auto"

答案 2 :(得分:7)

根据有关Android问题跟踪器的讨论,如果不创建自定义绑定适配器,则无法使用数据绑定设置布局高度或宽度:

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

设置视图高度所需的绑定适配器将如下所示:

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

答案 3 :(得分:1)

I solved it like this in Kotlin.

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"/>

答案 4 :(得分:0)

对我来说这就是诀窍:

    @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);
    }

和xml:

android:layout_height="@{item.isItemCollapsed ? @dimen/radar_item_first_background_height  : item.textMaxLines, default=@dimen/radar_item_first_background_height}"

答案 5 :(得分:0)

您可以为 android:layout_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);
    }

对于宽度:

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

用法

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 : 不要忘记设置默认值

答案 6 :(得分:0)

这是一种使用布尔值在两个 layout_heightlayout_width 状态之间切换的简洁、可扩展的机制。您需要将 LayoutWidthHeightBindingAdapterVariants 类导入到布局中以在布局中指定其值之一:

@BindingAdapter(value = ["android:layout_height", "app:layout_widthHeightVariant"])
fun expandContractHeight(view: View, flag: Boolean, variant: LayoutWidthHeightBindingAdapterVariants) {
    val params = view.layoutParams
    params.height = if (flag) variant.valueWhenTrue else variant.valueWhenFalse
    view.layoutParams = params
    view.invalidate()
}

@BindingAdapter(value = ["android:layout_width", "app:layout_widthHeightVariant"])
fun expandContractWidth(view: View, flag: Boolean, variant: LayoutWidthHeightBindingAdapterVariants) {
    val params = view.layoutParams
    params.height = if (flag) variant.valueWhenTrue else variant.valueWhenFalse
    view.layoutParams = params
    view.invalidate()
}

enum class LayoutWidthHeightBindingAdapterVariants(val valueWhenFalse: Int, val valueWhenTrue: Int) {
    // format: <falseValue>_TO_<trueValue>
    WRAP_TO_MATCH(WRAP_CONTENT, MATCH_PARENT),
    MATCH_TO_WRAP(MATCH_PARENT, WRAP_CONTENT),
    WRAP_TO_ZERODP(WRAP_CONTENT, 0),
    ZERODP_TO_WRAP(0, WRAP_CONTENT)
}