中心将在一个RelativeLayout的多个项目,而不将它们放在一个容器? [英] Center multiple items in a RelativeLayout without putting them in a container?

查看:132
本文介绍了中心将在一个RelativeLayout的多个项目,而不将它们放在一个容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一对并排侧键,这是我想被布置在中心的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_centerHorizo​​ntal =真
    />
  <按钮
    机器人: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>

这篇关于中心将在一个RelativeLayout的多个项目,而不将它们放在一个容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆