android - 使用数据绑定(bind)将百分比分配给 PercentFrameLayout

标签 android android-databinding android-percent-library percent-support-library

尝试使用百分比支持库创建带有布局的简单图形,但我无法弄清楚如何分配百分比以通过数据绑定(bind)进行查看。尝试返回字符串“60%”、float 0.6f、Float 0.6f、int 60。没有任何效果。

 <View
   android:id="@+id/graph_before"
   android:layout_height="@dimen/list_graph_line_height"
   android:layout_gravity="center_vertical"
   android:background="@color/colorAccent"
   app:layout_marginLeftPercent="@{item.percentValueLeft}"
   app:layout_widthPercent="@{item.percentValueWidth}"
   />

最佳答案

实现此目的的方法是在数据绑定(bind)和 PercentLayoutInfo 中使用自定义 setter :

@BindingAdapter("app:layout_widthPercent")
public static void setWidthPercent(View view, float width) {
    PercentLayoutHelper.PercentLayoutParams params =(PercentLayoutHelper.PercentLayoutParams) view.getLayoutParams();
    PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
    info.weightPercent = width;
}

关于android - 使用数据绑定(bind)将百分比分配给 PercentFrameLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35921922/

相关文章:

java - Android Java 在单独的线程中运行类似于 C# 风格的 Action 列表

android - MainActivityBinding.inflate(getLayoutInflater()) 页面未更新

android - Drawable 已经属于另一个所有者,但不公开常量状态

java - Percent 库上的 Android 动画

android - 开关元素未在分配的空间内水平居中对齐

android - Google Maps Android API v2 不显示 map

Android "WIN DEATH"无警告

android - 如何更改操作栏文本颜色?

android - 未知属性 onItemSelected/onCheckedChanged