綁定View的(或任何其他Android控件)權重的最簡單方法是什麼?因爲這個屬性沒有一個二傳手,我想自定義綁定,但ID似乎不工作:在設置如何綁定到視圖的MvvmCross中的layout_weight?
public class ViewWeightCustomBinding : MvxAndroidTargetBinding
{
public ViewWeightCustomBinding(object target) : base(target)
{
}
public override Type TargetType
{
get { return typeof (int); }
}
protected override void SetValueImpl(object target, object value)
{
var realTarget = target as View;
if(target == null)
return;
ViewGroup.LayoutParams layoutParameters = realTarget.LayoutParameters;
realTarget.LayoutParameters = new LinearLayout.LayoutParams(layoutParameters.Width, layoutParameters.Height,
(int) value);
}
}
登記:
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterFactory(new MvxSimplePropertyInfoTargetBindingFactory(typeof(ViewWeightCustomBinding), typeof(View), "ViewWeight"));
base.FillTargetFactories(registry);
}
而且.axml
<View
android:layout_width="0dp"
android:layout_height="3dp"
android:background="@color/green_holo"
local:MvxBind="ViewWeight Id" />
我可以在調試窗口看到Waring:
[0:] MvxBind:Warning:5.20無法爲綁定ViewWeight創建Id的目標綁定 [0:] MvxBind:Warning:5.20無法爲綁定ViewWeight創建目標綁定Id 01-31 10:54:57.247 I/mono-stdout (3795):MvxBind:警告:5.20未能創建目標結合ViewWeight Id的
據我所知'layout_weight'可使用以下代碼編程式設置:http://stackoverflow.com/questions/3224193/set-the-layout-weight-of-a-textview-programmatically - 所以你可以建立一個自定義綁定來支持這個。但是,我不知道佈局已經顯示後是否可以輕鬆更改權重值。 – Stuart
這是可以改變佈局後顯示的重量,我測試它在Java Android和它是好的。我在自定義綁定中使用了完全相同的代碼,但它不起作用,看起來像綁定本身存在問題。 – user963935