Xamarin.Android:DrawerLayout.LayoutParams 中的重力参数 [英] Xamarin.Android : Gravity parameter in DrawerLayout.LayoutParams
问题描述
我正在尝试使用 Xamarin.Android 以编程方式创建一个 DrawerLayout
,但是在尝试添加 ListView
时遇到了问题应该可以从左边拖动..
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 ..
这是我的代码:
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
参数
As you can notice.. I didn't know what to pass as a gravity
parameter
DrawerLayout
类中的函数定义是这样的:
The definition of the function in DrawerLayout
class is like that :
public LayoutParams(int width, int height, int gravity);
所以我必须为 gravity
传递 int
,但是怎么做 ??
So I have to pass int
for the gravity
, but how ??
我尝试了以下方法:
myListView.LayoutParameters = new DrawerLayout.LayoutParams( width: 240, height: DrawerLayout.LayoutParams.MatchParent, gravity: Gravity.LEFT);
它给了我错误:
重力"不包含左"的定义
'Gravity' does not contain a definition for 'LEFT'
也试过:
myListView.LayoutParameters = new DrawerLayout.LayoutParams( width: 240, height: DrawerLayout.LayoutParams.MatchParent, gravity: GravityFlags.Left);
但给了我一个错误:
无法从Android.Views.GravityFlags"转换为int"
cannot convert from 'Android.Views.GravityFlags' to 'int'
希望您对解决方案有所了解...并提前致谢
Hope you have an idea about the solution .. and thanks in advance
推荐答案
你必须像 int 一样转换标志
You have to cast the flag to int like
new DrawerLayout.LayoutParams(240, DrawerLayout.LayoutParams.WrapContent, (int)(GravityFlags.Start | GravityFlags.Left)).
这篇关于Xamarin.Android:DrawerLayout.LayoutParams 中的重力参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!