2016-02-11 67 views
0

我仍然是初學者在不同的Android佈局。這裏是簡單的線性佈局:Android LinearLayout將內容對齊到不同的側面

<LinearLayout 
     android:layout_height="50dp" 
     android:layout_width="fill_parent" 
     android:orientation="horizontal" 
     android:gravity="end" 
     android:padding="5dp"> 

     <ImageButton 
      android:id="@+id/bt_template" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_template_button" 
      android:scaleType="fitCenter" 
      android:background="#ffffffff" 
      android:contentDescription="Choose template"/> 

     <ImageButton 
      android:id="@+id/bt_captureImage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_camera_button" 
      android:scaleType="fitCenter" 
      android:background="#ffffffff" 
      android:contentDescription="Choose template"/> 

     <Button 
      style="@style/MyRedButton" 
      android:layout_margin="0dp" 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent" 
      android:text="@string/str_new_mail_activity_button_send" 
      android:id="@+id/bt_send" /> 

    </LinearLayout> 

我想要2個按鈕向左走,第三個按鈕向右走。我怎麼做?

enter image description here

藍色按鈕必須在左側和紅色按鈕右側。我怎樣才能做到這一點?

+1

你爲什麼不使用RelativeLayout的? –

+0

嘗試在LinearLayout中使用RelativeLayout。將你的按鈕放在RelativeLayout中,將它們的邊對齊,給它們留下左/右邊距。或者,您可以簡單地將紅色按鈕與RelativeLayout的右側以及左側的其他按鈕對齊。 –

回答

0

這是使用LinearLayout一個可能的選項:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:gravity="center_vertical" 
    android:layout_height="50dp" 
    android:layout_width="match_parent" 
    android:orientation="horizontal" 
    android:padding="5dp"> 

    <ImageButton 
     android:background="#ffffffff" 
     android:contentDescription="Choose template" 
     android:id="@+id/bt_template" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:scaleType="fitCenter" 
     android:src="@drawable/ic_template_button" /> 

    <ImageButton 
     android:background="#ffffffff" 
     android:contentDescription="Choose template" 
     android:id="@+id/bt_captureImage" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:scaleType="fitCenter" 
     android:src="@drawable/ic_camera_button" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

    <Button 
     style="@style/MyRedButton" 
     android:id="@+id/bt_send" 
     android:layout_height="match_parent" 
     android:layout_margin="0dp" 
     android:layout_width="wrap_content" 
     android:text="@string/str_new_mail_activity_button_send" /> 

</LinearLayout> 
0

如果您必須使用LinearLayout,請給紅色按鈕權重1(這樣會佔用空間),並確保它具有向右的重力,如果這是您需要的。

android:layout_weight="1" 
android:layout_gravity="right" 
0

檢查了這一點:

<RelativeLayout 
    android:layout_height="50dp" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    android:gravity="end" 
    android:padding="5dp"> 

    <ImageButton 
     android:id="@+id/bt_template" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" 
     android:scaleType="fitCenter" 
     android:background="#ffffffff" 
     android:layout_alignParentLeft="true" 
     android:contentDescription="Choose template"/> 

    <ImageButton 
     android:id="@+id/bt_captureImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" 
     android:scaleType="fitCenter" 
     android:layout_toRightOf="@id/bt_template" 
     android:background="#ffffffff" 
     android:contentDescription="Choose template"/> 

    <Button 

     android:layout_margin="0dp" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="send" 
     android:layout_alignParentRight="true" 
     android:id="@+id/bt_send" /> 

</RelativeLayout>