Android视图布局

Android视图布局,android,android-layout,Android,Android Layout,我正在尝试在我的android应用程序顶部创建一个栏,它有三个按钮和一个徽标,最左侧有一个按钮,徽标居中,右侧有两个按钮。我的问题是右侧的两个按钮重叠,如何使它们都位于右侧但不堆叠 以下是视图的XML结构: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout

我正在尝试在我的android应用程序顶部创建一个栏,它有三个按钮和一个徽标,最左侧有一个按钮,徽标居中,右侧有两个按钮。我的问题是右侧的两个按钮重叠,如何使它们都位于右侧但不堆叠

以下是视图的XML结构:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent" 
          android:layout_height="42px" 
          android:orientation="horizontal"
          android:background="@drawable/header_background">

    <RelativeLayout
         android:layout_width="fill_parent"
         android:layout_height="fill_parent" >

    <Button android:id="@+id/button_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:background="@drawable/menu_button"
        android:layout_gravity="left"
        android:layout_alignParentLeft="true" />

    <ImageView
            android:id="@+id/imageViewLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo_white" 
            android:layout_centerInParent="true"/>

    <Button android:id="@+id/button_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:background="@drawable/camera_button"
        android:layout_gravity="right"
        android:layout_alignParentRight="true" />

    <Button android:id="@+id/button_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:background="@drawable/two_up_button"
        android:layout_gravity="right"
        android:layout_alignParentRight="true" />

    </RelativeLayout>
</LinearLayout>


任何建议都很好!谢谢。

两个
按钮\u开关和
按钮\u摄像头
都在父级内部对齐,因为明显的属性说明:
android:layout\u alignParentRight=“true”


使用属性
layout\u toLeftOf
layout\u toRightOf
详细指定

我认为您的第一个按钮(
按钮\摄像机
)需要使用
布局\按钮\开关
对准
开关的右侧。
只需要另一个相对位置即可-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent" 
          android:layout_height="42px" 
          android:orientation="horizontal"
          android:background="@drawable/header_background">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    <Button android:id="@+id/button_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:background="@drawable/menu_button"
        android:layout_gravity="left"
        android:layout_alignParentLeft="true" />

    <ImageView
            android:id="@+id/imageViewLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bloom_logo_white" 
            android:layout_centerInParent="true"

            />

    <Button android:id="@+id/button_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:background="@drawable/camera_button"
        android:layout_gravity="right"
        android:layout_alignParentRight="true" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/button_camera">

            <Button android:id="@+id/button_switch"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text=""
       android:background="@drawable/two_up_button"
       android:layout_gravity="right"
       android:layout_alignParentRight="true" />
    </RelativeLayout>

</RelativeLayout>
</LinearLayout>


应尽可能避免嵌套容器布局。RelativeLayout中的值是它为其子级提供的特殊属性(请参阅其他答案)允许嵌套最少的复杂布局。虽然这个答案可能会达到您想要的结果,但它违背了最佳做法,因为嵌套太深的布局可能会导致性能问题并使代码复杂化。事实上,示例代码中的外部LinearLayout可能应该删除,因为它唯一的子项是RelativeLayout,它占用了家长的所有空间。没问题,你走对了!请查看此链接:这是谷歌为新手开发人员提供的培训页面。你可以在那里找到一些基本的布局技巧,以及构建应用程序的其他信息。它信息量大,可以真正帮助你以Android-y的方式思考问题。