Xamarin.Android : Gravity parameter in DrawerLayout.LayoutParams

a 夏天 提交于 2019-12-20 05:20:22

问题


I'm trying to create a DrawerLayout programmatically using Xamarin.Android, but I faced a problem when trying to add the ListView which should be dragable form the left ..

Here is my code :

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);

As you can notice.. I didn't know what to pass as a gravity parameter

The definition of the function in DrawerLayout class is like that :

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

So I have to pass int for the gravity, but how ??

I've tried the following :

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

It gives me error:

'Gravity' does not contain a definition for 'LEFT'

Also tried :

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

but gives me an error:

cannot convert from 'Android.Views.GravityFlags' to 'int'

Hope you have an idea about the solution .. and thanks in advance


回答1:


You have to cast the flag to int like

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



回答2:


Since DrawerLayout is a Class from the Support Packages you can use GravityCompat:

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


来源:https://stackoverflow.com/questions/39012568/xamarin-android-gravity-parameter-in-drawerlayout-layoutparams

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!