1

嘗試使用百分比支持庫創建帶佈局的簡單圖形,但我無法弄清楚,如何通過數據綁定來指定百分比來查看。嘗試返回字符串「60%」,浮動0.6f,浮動0.6f,詮釋60.沒有任何作品。使用數據綁定將百分比分配給PercentFrameLayout

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

回答

2

辦法做到這一點是通過使用數據綁定和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; 
}