2013-03-12 68 views
0

我正在嘗試在我的Android應用程序頂部創建一個條,該條有三個按鈕和一個徽標,最左側的一個按鈕,中心的徽標和右側的兩個按鈕。我的問題是,右側的兩個按鈕重疊,我怎麼讓他們都在右側,但沒有堆疊?Android View Layout

這裏是我認爲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> 

任何意見將是巨大的!謝謝。

回答

1

兩個button_switchbutton_camera作爲明顯的屬性規定的權利父裏面排列:android:layout_alignParentRight="true"

使用屬性layout_toLeftOflayout_toRightOf詳細地說明。

+0

感謝您的幫助 – 2013-03-12 15:20:47

1

我認爲您的第一個按鈕(button_camera)需要使用layout_toRightOf="@id/button_switchbutton_switch對齊。

+0

感謝您的幫助 – 2013-03-12 15:19:57

-1

只是需要另一個RelativeLayout-

<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> 
+0

嵌套容器的佈局應儘可能避免。 RelativeLayout中的值是它爲其子代提供的特殊屬性(請參閱其他答案),以便在嵌套最小的情況下實現複雜的佈局。雖然這個答案可能會達到你想要的結果,但是這不符合最佳實踐,因爲嵌套得太深可能會導致性能問題並使代碼複雜化。實際上,示例代碼中的外部LinearLayout可能應該被刪除,因爲它的唯一子對象是一個RelativeLayout,它佔用了父級的所有空間。 – MattDavis 2013-03-12 15:27:21

+0

感謝馬特,很高興知道。 noob到android佈局世界 – 2013-03-12 15:32:39

+0

沒問題,你在正確的軌道上!看看這個鏈接:http://developer.android.com/training/index.html這是谷歌的培訓頁面,爲初學者開發。你可以找到一些基本的佈局提示,以及在那裏構建應用程序的其他信息。它的內容非常豐富,可以真正幫助您以Android-y的方式思考問題。 – MattDavis 2013-03-12 15:41:29