簡體   English   中英

Android 數據綁定 - 如何從 dimens.xml 獲取維度

[英]Android databinding - How to get dimensions from dimens.xml

我想根據我在 dimens.xml 中創建的尺寸設置邊距它自己的尺寸工作正常,它只是數據綁定無法在下面的情況下找到它:

<TextView
           android:id="@+id/title_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/disableButton"
*************
        android:layout_marginBottom="@{@bool/showAds ? 
@dimen/frontpage_margin_ads: @dimen/frontpage_margin_noads}"
*************        
android:gravity="center_horizontal"
        android:text="@string/app_name"
        android:textColor="@android:color/holo_orange_dark"
        android:contentDescription="@string/app_name"
        android:textSize="64sp"
        android:textStyle="bold" />

它確實找到了它,但它說 marginbottom 不能采用 float 類型。 我怎樣才能解決這個問題? 我嘗試將兩個維度都轉換為 int,但隨后它抱怨無法將其轉換為 int。

我的尺寸 xml 文件如下所示:

    <resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="bigText">44sp</dimen>
    <dimen name="littleText">44sp</dimen>
    <dimen name="mediumText">40sp</dimen>
        <dimen name="smallText">24sp</dimen>
    <dimen name="fab_margin">16dp</dimen>
    <dimen name="frontpage_margin_noads">0dp</dimen>
    <dimen name="frontpage_margin_ads">13dp</dimen>


</resources>

這里的問題不在於尺寸,而android:layout_marginBottom 沒有對任何LayoutParams屬性的內置支持。 這樣做是為了移除許多人可能用來將變量綁定到LayoutParams的“腳槍”,並可能嘗試使用數據綁定以這種方式為其位置設置動畫。

數據綁定非常適合在您的示例中使用,您可以輕松添加自己的。 它會是這樣的。

@BindingAdapter("android:layout_marginBottom")
public static void setBottomMargin(View view, float bottomMargin) {
    MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
    layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin,
        layoutParams.rightMargin, Math.round(bottomMargin));
    view.setLayoutParams(layoutParams);
}

當然,您還可以添加 left、top、right、start 和 end BindingAdapters

幾乎相同的解決方案,但使用 Kotlin:

在文件 BindingAdapters.kt 中添加:

@BindingAdapter("layoutMarginBottom")
fun setLayoutMarginBottom(view: View, dimen: Float) {
    view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
        bottomMargin = dimen.toInt()
    }
}

布局中的用法:

<LinearLayout
    app:layoutMarginBottom="@{viewModel.type == Type.SMALL ? @dimen/margin_small : @dimen/margin_large}"

您可以為topstartend邊距編寫類似的方法。

其他例子對我不起作用,所以我自己寫了一個。 這可以添加到一個新的空白 Kotlin 文件中。 這些函數不需要在一個類中。 確保在文件頂部有您自己的包聲明。

import android.view.View
import android.view.ViewGroup
import androidx.databinding.BindingAdapter

@BindingAdapter("android:layout_marginBottom")
fun setLayoutMarginBottom(view: View, dimen: Int) {
    (view.layoutParams as ViewGroup.MarginLayoutParams).let {
        it.bottomMargin = dimen
        view.layoutParams = it
    }
}

@BindingAdapter("android:layout_marginTop")
fun setLayoutMarginTop(view: View, dimen: Int) {
    (view.layoutParams as ViewGroup.MarginLayoutParams).let {
        it.topMargin = dimen
        view.layoutParams = it
    }
}

@BindingAdapter("android:layout_marginStart")
fun setLayoutMarginStart(view: View, dimen: Int) {
    (view.layoutParams as ViewGroup.MarginLayoutParams).let {
        it.marginStart = dimen
        view.layoutParams = it
    }
}

@BindingAdapter("android:layout_marginEnd")
fun setLayoutMarginEnd(view: View, dimen: Int) {
    (view.layoutParams as ViewGroup.MarginLayoutParams).let {
        it.marginEnd = dimen
        view.layoutParams = it
    }
}

暫無
暫無

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

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