Android数据绑定layout_width和layout_height [英] Android Data Binding layout_width and layout_height

查看:141
本文介绍了Android数据绑定layout_width和layout_height的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 binding to control the height of my elements. Here is a stripped down version of my 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 Model的一个精简版本:

And here is a stripped down version of my View Model:

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

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

The dimensions are in the dimens.xml file. And I am modifying the properties in the view model. But, when I launch the app, I'm getting the following error immediately after launch (the bindingadapter is not firing on debug). I have several other elements on the screen but this particular one is the one I need to change the height when a particular action occurs:

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的帖子,但没有明确的答案或方法不起作用。当然这是一个常见的实现。感谢您的帮助。

There are a few posts on SO regarding this issue but no definitive answers or the approach did not work. Surely this is an implementation that is common. Thanks in advance for the help.

推荐答案

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

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.

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

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

这篇关于Android数据绑定layout_width和layout_height的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆