Xamarin.Android:DrawerLayout.LayoutParams

时间:2016-08-18 07:46:18

标签: c# android xamarin.android drawerlayout

我正在尝试使用 Xamarin.Android 以编程方式创建DrawerLayout,但在尝试添加应该可以从左侧拖动的ListView时遇到了问题..

这是我的代码:

DrawerLayout myDrawerLayout = new DrawerLayout(this);
myDrawerLayout.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
SetContentView(myDrawerLayout);


FrameLayout myFrameLayout = new FrameLayout(this);
myFrameLayout.LayoutParameters = new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
myFrameLayout.SetBackgroundColor(Android.Graphics.Color.Gray);
myDrawerLayout.AddView(myFrameLayout);


ListView myListView = new ListView(this);
myListView.SetBackgroundColor(Android.Graphics.Color.Green);
myListView.LayoutParameters = new DrawerLayout.LayoutParams( width: 240,
                                                  height: DrawerLayout.LayoutParams.MatchParent,
                                                  gravity: ????????
                                                           );
myDrawerLayout.AddView(myListView);

你可以注意到......我不知道作为gravity参数传递什么

DrawerLayout类中函数的定义是这样的:

public LayoutParams(int width, int height, int gravity);

所以我必须为int传递gravity,但是如何?

我尝试了以下内容:

myListView.LayoutParameters = new DrawerLayout.LayoutParams( width: 240, height: DrawerLayout.LayoutParams.MatchParent, gravity: Gravity.LEFT);

它给了我错误:

  

'Gravity'不包含'LEFT'的定义

也尝试过:

myListView.LayoutParameters = new DrawerLayout.LayoutParams( width: 240, height: DrawerLayout.LayoutParams.MatchParent, gravity: GravityFlags.Left);

但是给了我一个错误:

  

无法从'Android.Views.GravityFlags'转换为'int'

希望您对解决方案有所了解......并提前感谢

2 个答案:

答案 0 :(得分:4)

你必须将标志强制转换为int,如

new DrawerLayout.LayoutParams(240, DrawerLayout.LayoutParams.WrapContent, (int)(GravityFlags.Start | GravityFlags.Left)).

答案 1 :(得分:0)

由于DrawerLayout是支持包中的一个类,因此您可以使用GravityCompat:

   myListView.LayoutParameters = new DrawerLayout.LayoutParams(width: 240,
                         height: DrawerLayout.LayoutParams.WrapContent, 
                         gravity: GravityCompat.Start);