Android Data Binding layout_width and layout_height Android数据绑定的layout_width和layout_height

5 浏览
0 Comments

Android Data Binding layout_width and layout_height Android数据绑定的layout_width和layout_height

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




    



以下是我View Model的简化版本:

public class LoginViewModel extends BaseObservable {
public final ObservableField verificationCode; 
public final ObservableField 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文件中。我正在修改视图模型中的属性。但是,当我启动应用程序后,我立即收到以下错误(在调试时绑定适配器没有触发)。屏幕上有几个其他元素,但这个特定的元素是我需要在特定操作发生时更改高度的元素:

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

0