簡體   English   中英

Android 數據綁定 layout_width 和 layout_height

[英]Android Data Binding layout_width and 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 的帖子,但沒有明確的答案或方法不起作用。 當然,這是一個常見的實現。 在此先感謝您的幫助。

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

當使用數據綁定時,我們從 XML 中剝離值。 您可以添加要在剝離時使用的默認值以避免該問題。

請參閱: http : //developer.android.com/tools/data-binding/guide.html (頁面底部)。

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

根據 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);
}

我在Kotlin而不是Java 中解決了這個問題。

制作一個名為

MyBindings.kt

並輸入:

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

然后你可以在你的布局中使用它:

<EditText 
android:id="@+id/internalTopDisplay"
android:layout_width="match_parent"
bind:customHeight="@{loginVM.compact ? @dimen/verificationHeightCompact : @dimen/verificationHeightFull}"
android:layout_height="wrap_content"/>

對我來說,這就是訣竅:

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

您可以為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 :不要忘記設置默認值

這是一種使用布爾值在兩個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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM