我有一个包含一对并排侧键,这是我想被布置在中心的RelativeLayout的。我可以把按钮放在一个的LinearLayout和中心,在RelativeLayout的,但我想保持我的XML尽可能干净。
下面是我的尝试,这只是把应用按钮,在中心和取消按钮,它的左:
< RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =15sp>
<的TextView
机器人:ID =@ + ID /指令
机器人:文本=@字符串/指令
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =15sp
机器人:layout_centerHorizontal =真
/>
<按钮
机器人:ID =@ + ID /应用
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真
机器人:文本=@字符串/应用
机器人:TEXTSIZE =20SP
机器人:layout_below =@ ID /指令
/>
<按钮
机器人:ID =@ + ID /撤销
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真
机器人:文本=@字符串/撤销
机器人:TEXTSIZE =20SP
机器人:layout_toLeftOf =@ ID /应用
机器人:layout_below =@ ID /指令
/>
< / RelativeLayout的>
解决方案
安卓重力
将调整查看里面的内容
或布局
它用于
安卓layout_gravity
将调整视图
或布局
他的父母的内部。
所以添加
安卓重力=中心
你的RelativeLayout的应该做的伎俩......
这样的:
< RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:重力=中心
机器人:layout_marginTop =15sp>
< / RelativeLayout的>
I have a RelativeLayout containing a pair of side-by-side buttons, which I want to be centered within the layout. I could just put the buttons in a LinearLayout and center that in the RelativeLayout, but I want to keep my xml as clean as possible.
Here's what I tried, this just puts the "apply" button in the center and the "undo" button to the left of it:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp">
<TextView
android:id="@+id/instructions"
android:text="@string/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15sp"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/apply"
android:textSize="20sp"
android:layout_below="@id/instructions"
/>
<Button
android:id="@+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/undo"
android:textSize="20sp"
android:layout_toLeftOf="@id/apply"
android:layout_below="@id/instructions"
/>
</RelativeLayout>
解决方案
android:gravity
will align the content inside the view
or layout
it is used on.
android:layout_gravity
will align the view
or layout
inside of his parent.
So adding
android:gravity="center"
to your RelativeLayout should do the trick...
Like this:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="15sp">
</RelativeLayout>
上一篇:如何让Android的屏幕尺寸编程方式,一劳永逸?方式、屏幕尺寸、Android、一劳永逸
下一篇:安卓invalidateOptionsMenu()的API&LT; 11API、invalidateOptionsMenu、LT