简体   繁体   English

Android数据绑定从父级宽度设置layout_width

[英]android data binding set layout_width from parent width

How to set the child view's width using data binding. 如何使用数据绑定设置子视图的宽度。 The value to set is dynamic and it depends on the width of the parent layout. 要设置的值是动态的,并且取决于父布局的宽度。

item_bar.xml item_bar.xml

<LinearLayout
    android:id="@+id/barLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="@{model.usedBarWidthPercentage}"
        android:layout_height="4dp"/>
</LinearLayout>

The model can give me a percentage. 该模型可以给我一个百分比。 Ex, if the percentage is 40%, it means the width of the textview should be 40% of the parent layout. 例如,如果百分比为40%,则表示textview的宽度应为父版面的40%。

I know the idea of using data binding adapters, but dont know how to do it with the parent layout's width. 我知道使用数据绑定适配器的想法,但不知道如何使用父布局的宽度来做到这一点。

You can create a class file, example MyBingding.class 您可以创建一个类文件,例如MyBingding.class

@BindingAdapter({ "bindWidth" })
public static void bindWidth(TextView textView, double perc) {
    //You can do something by java code here 
    textView.xxxxxxxx; 
}

Then use bindWidth method in XML: 然后在XML中使用bindWidth方法:

<TextView
    app:bindWidth="@{model.usedBarWidthPercentage}"
    android:layout_width="wrap_content"
    android:layout_height="4dp"/>

Make sure the usedBarWidthPercentage's data type is same as bindWidth method's perc. 确保usedBarWidthPercentage的数据类型与bindWidth方法的perc相同。

I'm afraid you will be disappointed on this one iori24. 恐怕您会对这个iori24感到失望。 I tried for qhite a while to figure out how to accomplish this, but some variables can be done post draw and some have to be done pre draw. 我尝试了一段时间,以弄清楚如何完成此操作,但是某些变量可以在绘制后完成,而某些变量必须在绘制前完成。 Binding is meant to be for post draw type of values. 绑定旨在用于值的绘制后类型。 layout_width and layout_height are pre draw values that need to be set ahead of time. layout_width和layout_height是需要预先设置的绘制前值。

Unfortunately there is no current way to do this. 不幸的是,目前尚无此方法。 I found many articles on it and can confirm this statement. 我发现了很多文章,可以证实这一说法。 However, I managed to find ways to manipulate my UI with various match parents or weights to accomplish what I needed or adjust in code. 但是,我设法找到了使用各种匹配的父项或权重来操作UI的方法,以完成我需要的内容或在代码中进行调整。

I would love if they brought data binding to layout_width and height, and I think they will eventually, but for now you will just have to get more creative with your design. 如果它们将数据绑定带到layout_width和height上,我会很乐意,我认为它们最终会成功,但是就目前而言,您将只需要对您的设计更具创意。

If you would like you could provide your full sample code for someone to modify and hand back with potential fixes, but I'm guessing you will need a code option here and not an XML option. 如果您希望提供完整的示例代码供某人进行修改并提供潜在的修复程序,但我想您这里将需要一个代码选项,而不是一个XML选项。 Now namezhouyu posted a binding adapter option. 现在namezhouyu发布了绑定适配器选项。 This is a postdraw that will take the textview and potentially modify it's value after it has been drawn. 这是一个后绘制,将使用textview并在绘制后可能修改其值。 This could work, but you may see strange behavior as the original size would be wrap_content and then each subsequent change would cause it to jump to correct size. 这可能会起作用,但是您可能会看到奇怪的行为,因为原始大小将为wrap_content,然后每个后续更改都将导致其跳转为正确的大小。 But I do like his work around, it is a clever solution. 但是我确实喜欢他的工作,这是一个聪明的解决方案。

So if you are bound and determined to do it through binding, then I would follow namez houyu's option. 因此,如果您受约束并决心通过绑定来做到这一点,那么我将遵循namez houyu的选择。

What I end up doing is follow @Raghunandan's suggestion 我最终要做的是遵循@Raghunandan的建议

class ViewHolder(val binding: ItemViewBinding) : RecyclerView.ViewHolder(binding.root) {

    fun bind(model: Model) {
        Logger.d("START")
        binding.model = model

        val treeObserver = binding.viewLayout.viewTreeObserver
        if (treeObserver.isAlive) {
            treeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
                override fun onGlobalLayout() {
                    binding.viewLayout.viewTreeObserver.removeOnGlobalLayoutListener(this) // need to call viewTreeObserver again
                    val maxValue = binding.viewLayout.width
                    val usePerc = binding.model.getUsedBarWidthPercentage()
                    Logger.d("maxValue=$maxValue, usePerc=$usePerc")
                    binding.view.layoutParams.width = (maxValue * usePerc).toInt()
                }
            })
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 数据绑定 layout_width 和 layout_height - Android Data Binding layout_width and layout_height 如果由android设置,则getWidth()返回0:layout_width =“match_parent” - getWidth() returns 0 if set by android:layout_width=“match_parent” 如何从代码中设置android:layout_width =“match_parent”? - How to set android:layout_width=“match_parent” from code? 在Android中以编程方式设置EditText的layout_width - Programatically set layout_width of EditText in Android 生成的约束布局中的Android layout_width=&quot;match_parent&quot;? - Android layout_width="match_parent" in generated constraint layout? 如何在Android中使用数据绑定绑定layout_width和layout_height - How to bind a layout_width and layout_height using data binding in Android Android RelativeLayout和子元素,layout_width =“match_parent” - Android RelativeLayout and childs with layout_width=“match_parent” TextView字幕无需android:layout_width =“ match_parent” - TextView marquee WITHOUT android:layout_width=“match_parent” 如何在android中实用地设置图像的layout_width - How to set layout_width of an image pragmatically in android TableLayout with layout_width = matchparent不匹配parent - TableLayout with layout_width=matchparent not matching parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM