如何在viewGroup中加载Switch?

时间:2015-03-16 08:32:09

标签: android xamarin xamarin.android

我试图在一个viewGroup中加载一个Switch ,我所做的就是创建一个包含frameLayout的LinearLayout。在代码中我创建了一个类" CustomViewGroup"从ViewGroup扩展并将其添加为FrameLayout的子级。 CustomViewGroup将切换为其子但加载时可能无法查看。我错过了什么?

我附上了我的代码:

的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <FrameLayout
        android:id="@+id/myBaseLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </FrameLayout>
</LinearLayout>

活性:

public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.Main);
            FrameLayout mylayout = FindViewById<FrameLayout> (Resource.Id.myBaseLayout);
            CustomViewGroup cell = new CustomViewGroup (BaseContext);
            Switch myswitch = new Switch (BaseContext);
            myswitch.Checked = true;
            cell.AddView (myswitch);
            mylayout.AddView (cell);
        }
    }

CustomViewGroup:

public class CustomViewGroup : ViewGroup
    {
        public CustomViewGroup (Context context) : base (context)
        {
        }

        protected override void OnLayout (bool changed, int l, int t, int r, int b)
        {
            var view = GetChildAt (0);
            if (view != null)
                view.Layout (0, 0, this.Width, 40);
        }
    }

0 个答案:

没有答案