Android 数据绑定 layout_width 和 layout_height

新手上路,请多包涵

我需要能够动态设置 EditText 的高度属性。我在整个应用程序中为其他属性使用数据绑定,因此我希望能够使用数据绑定来控制元素的高度。这是我的 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>

这是我的视图模型的精简版:

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

 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 上有一些关于此问题的帖子,但没有明确的答案或该方法无效。当然,这是一个常见的实现。在此先感谢您的帮助。

原文由 John Murphy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 857
2 个回答

在爪哇

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

原文由 Linh 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

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

原文由 yigit 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题