2

如何使用RelativeLayoutAndroid中获得此布局?无论我尝试什么,前2个按钮都不会按预期显示。下面我提供了我想要的东西,我尝试的东西以及我的尝试输出的截图。请任何人都可以帮助我?谢谢。如何在Android中实现这种布局?

enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#f5f5f5" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button3" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button1" 
     android:text="Button" /> 

</RelativeLayout> 

更新时间:

任何帮助深表感谢。

+1

为什么使用'RelativeLayout'?你可以很容易地用一个外部垂直'LinearLayout'来实现,这个'LinearLayout'包含一个用于按钮1和2的水平'LinearLayout',然后是按钮3和按钮4.只需将'layout_weight'设置为水平LinearLayout的1/8/1;按钮3/4分别。 – Squonk 2015-02-24 15:27:40

回答

2

试试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:id="@+id/topLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:gravity="center" 
      android:textColor="#ffffff" 
      android:background="#ff6a2b" 
      android:text="Button 1" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:layout_marginLeft="1dp" 
      android:gravity="center" 
      android:textColor="#ffffff" 
      android:background="#ff6a2b" 
      android:text="Button 2" /> 

    </LinearLayout> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:gravity="center" 
     android:textColor="#ffffff" 
     android:background="#14b8ff" 
     android:text="Button 4" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/button4" 
     android:layout_below="@+id/topLayout" 
     android:gravity="center" 
     android:textColor="#ffffff" 
     android:background="#000000" 
     android:text="Button 3" /> 

</RelativeLayout> 

好运。

+0

嗨,谢谢它的作品,但按钮之间有空格,我可以删除它们吗? http://i.stack.imgur.com/Ls7RF.png谢谢。 – Henry 2015-02-24 15:38:57

+0

这是因为android自定义按钮背景。定义你的按钮可绘制或按钮背景色后,你会看到它们消失了。 – 2015-02-24 15:40:55

+0

@亨利看到我的编辑,我添加了背景颜色,他们走了。 – 2015-02-24 15:45:47

1

试试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#f5f5f5" 
tools:context=".MainActivity" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="2" 
    android:id="@+id/topButtons"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button1" 
    android:layout_weight="1"/> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button2" 
    android:layout_weight="1"/> 

</LinearLayout> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/topButtons" 
    android:layout_above="@+id/button4" 
    android:text="Button3" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Button4" 
    android:layout_alignParentBottom="true"/> 

+0

嗨,感谢它的工作,但按钮之间有空格,我可以删除这些? http://i.stack.imgur.com/Ls7RF.png谢谢。 – Henry 2015-02-24 15:39:13

1

你也可以使用一个无形的视图来达到同样的效果,而无需使用额外的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#f5f5f5" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Button" 
     android:layout_toStartOf="@+id/alignment" /> 

    <View 
     android:id="@+id/alignment" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" 
     android:visibility="invisible" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="Button" 
     android:layout_toEndOf="@+id/alignment" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button3" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button1" 
     android:text="Button" /> 

</RelativeLayout> 
+0

嗨,感谢它的作品,但按钮之间有空格,我可以删除这些? http://i.stack.imgur.com/Ls7RF.png谢谢。 – Henry 2015-02-24 15:40:35

+0

调整button1的xml中使用'android:layout_marginRight =“ - 4dp”'(或其他负数)和button2的xml中'android:layout_marginLeft =“ - 4dp”'(或其他负数)之间的距离。 – 2015-02-24 16:25:06

1

我会建议使用线性布局! 因为您似乎需要将按钮1和2放在最上方,并将按钮4放在最下方,并使按钮3占据所有剩余的空白区域。 为此,请在下面使用layout_weight的:我没有使用alignParent,layout_above或layout_below属性了,因为他们是无用的的LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#f5f5f5" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:id="@+id/header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </LinearLayout> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content 
     android:text="Button" /> 

</LinearLayout> 

通知。 另请注意,我已经指定了几个按钮的宽度或高度为0。 在这个问题上,他们将根据屏幕上剩余的空白空间进行计算。因此,无论运行应用程序的设备的屏幕尺寸如何,此布局都将保持兼容!

1

这里应该是解决方案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#f5f5f5" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:orientation="horizontal"> 


    <Button 
    android:id="@+id/button1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:text="Button1" /> 

    <Button 
    android:id="@+id/button2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_toRightOf="@+id/button1" 
    android:text="Button2" /> 
</RelativeLayout> 

    <Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentCenter="true" 
    android:text="Button3" /> 

    <Button 
    android:id="@+id/button4" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:text="Button4" /> 

</RelativeLayout> 

,你也需要添加android:background=颜色为每个按钮。