gpt4 book ai didi

Android数据绑定(bind)layout_width : You must supply a layout_width attribut

转载 作者:行者123 更新时间:2023-12-02 15:01:10 24 4
gpt4 key购买 nike

我正在尝试绑定(bind) View 的宽度和高度,但我看不出问题出在哪里。

我在this google issue上找到了这个

To implement these for your application, create a binding adapter:

@BindingAdapter("android:layout_width")
public static void setLayoutWidth(View view, int width) {
LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = width;
view.setLayoutParams(layoutParams);
}

所以我像这样创建了我的绑定(bind)适配器:

public class SimpleBindingAdapter {

@BindingAdapter("android:layout_width")
public static void setLayoutWidth(View view, int width) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = width;
view.setLayoutParams(layoutParams);
}

@BindingAdapter("android:layout_height")
public static void setLayoutHeight(View view, int height) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = height;
view.setLayoutParams(layoutParams);
}

//Others methods...
}

然后尝试像这样设置我的宽度和高度:

<View
android:layout_width="@{paramsMessage.width}"
android:layout_height="@{paramsMessage.height}"
... />

其中 paramsMessage.width 是公共(public) int 属性。

但我收到此错误:

Caused by: java.lang.RuntimeException: Binary XML file line #25: You must supply a layout_width attribute. at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:607) at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6761) at android.view.ViewGroup$MarginLayoutParams.(ViewGroup.java:6930) at android.widget.RelativeLayout$LayoutParams.(RelativeLayout.java:1244) at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1084) at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:83) at android.view.LayoutInflater.rInflate(LayoutInflater.java:820) at android.view.LayoutInflater.inflate(LayoutInflater.java:511) at android.view.LayoutInflater.inflate(LayoutInflater.java:415) at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:116) at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:88) at be.standard.appbusiness.tutorials.home.TutorialHomeFragment.onCreateDialog(TutorialHomeFragment.java:35) at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:308) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234) at android.app.Activity.performStart(Activity.java:6258) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)  at android.app.ActivityThread.access$900(ActivityThread.java:172)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:5832)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

我将不胜感激任何帮助,谢谢!

最佳答案

这已经晚了,但对于任何需要它的人,我做了以下操作:

我在 View 中将layout_width和layout_height设置为0dp。

<ImageView
android:layout_width="0dp"
android:layout_height="0dp" />

您可以将 minWidth/minHeight 与数据绑定(bind)一起使用,或创建自己的属性以通过绑定(bind)设置宽度和高度。

<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:minWidth="@{controller.getDeviceWidthDP()}"
android:minHeight="@{controller.getDeviceWidthDP()}" />

为每个属性创建 BindingAdapter,这里我使用 Kotlin。

object ViewBindings{
@JvmStatic
@BindingAdapter("android:minWidth")
fun setLayoutWidth(view: View, width: Float) {
val layoutParams = view.layoutParams
layoutParams.width = (width * view.resources.displayMetrics.density).toInt()
view.layoutParams = layoutParams
view.invalidate()
}

@JvmStatic
@BindingAdapter("android:minHeight")
fun setLayoutHeight(view: View, height: Float) {
val layoutParams = view.layoutParams
layoutParams.height = (height * view.resources.displayMetrics.density).toInt()
view.layoutParams = layoutParams
view.invalidate()
}
}

在此示例中,我使用一个扩展函数来获取设备宽度和高度尺寸。

fun Context.getDeviceDimensions(): Pair<Float, Float> {
var widthHeight = Pair(0.0F, 0.0F)
resources.displayMetrics.let {
val dpHeight = it.heightPixels / it.density
val dpWidth = it.widthPixels / it.density
widthHeight = Pair(dpWidth, dpHeight)
}

return widthHeight
}

我创建了一个 Controller 类并使用 <data>标记将其包含在我的布局中。

class SomeController(val someFragment: SomeFragment){
fun getDeviceWidthDP(): Float{
val width = someFragment.context.getDeviceDimensions().first
return width
}
}

关于Android数据绑定(bind)layout_width : You must supply a layout_width attribut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com