我在RelativeLayout中有一个ImageView,我以编程方式添加(ImageView是按代码添加的,而不是RelativeLayout。)
如果我通过XML添加了ImageView,我会这样做:
< ImageView的
机器人:ID = “@ + ID / login_loadingimage”
机器人:layout_width = “WRAP_CONTENT”
机器人:layout_height = “WRAP_CONTENT”
机器人:layout_centerHorizontal = “真”
机器人:layout_centerVertical = “真”
机器人:SRC = “@绘制/映像文件名称”
机器人:能见度=“隐形”
/>
但现在我希望以编程方式完成这项工作。我如何正确对中?我尝试过创建RelativeLaout.Parameters但是我使用
创建它们RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(this, null); //[1]
p.AddRule(...);[2]
第1行引发异常。另外,我不知道如何在ViewGroup.LayutParameters上应用这些LayoutParameters。
另外,我不能只是制作ViewGroup.LayoutParameters因为
它们似乎没有我需要的居中(水平和垂直)元素
2.每个例子似乎都表明有一个构造函数只是将Layout_Width和Layout_Height作为参数,但没有。
在有人说首先添加布局之前,请告诉我如何在代码中居中布局。感谢。
请帮忙。
答案 0 :(得分:3)
这样做的方法很简单:
创建你想要的xaml组件 - 将文件放在布局文件夹中。
然后使用膨胀服务来扩展所需代码内的控件(确保按如下方式创建LayoutInflator):
LayoutInflater _inflatorservice = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
然后简单地称之为:
_inflatorservice.Inflate(Resource.Layout.[control], parent);
鲍勃是你母亲的兄弟!