如何动态创建抽屉布局,而无需仅从代码中获取任何 xml 文件



我想动态创建抽屉布局,没有任何xml文件,仅来自代码 我找到了一些动态制作项目的示例 但我想动态创建一切,而不仅仅是项目 我能做到吗? 请帮助我

我发现代码运行

DrawerLayout drawerLayout;
FrameLayout drawerPane;
LinearLayout content;
//-
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//-----------------------------
content = new LinearLayout(this);
content.SetBackgroundColor(Color.White);
//-----------------------------
drawerPane = new FrameLayout(this);
drawerPane.SetForegroundGravity(GravityFlags.Start);
drawerPane.Clickable = true;
drawerPane.SetBackgroundColor(Color.Red);
//-
ListView myList = new ListView(this);
myList.ChoiceMode = ChoiceMode.Single;
myList.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, new List<string>() { "Item #1", "Item #2", "Item #3" });
drawerPane.AddView(myList, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
//-----------------------------
drawerLayout = new DrawerLayout(this);
drawerLayout.AddView(content, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
drawerLayout.AddView(drawerPane, new DrawerLayout.LayoutParams(240, DrawerLayout.LayoutParams.MatchParent, (int)GravityFlags.Start));
//-----------------------------
SetContentView(drawerLayout);
}

最新更新