2015-04-24 121 views
4

我有ImageButton in LinearLayout。我嘗試將ImageButton置於右側水平LinearLayout。我使用layout_gravity="right"但它不起作用。請幫幫我。如何將ImageButton對齊到水平LinearLayout

<LinearLayout 
    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:orientation="vertical" 
    android:weightSum="5" 
    tools:context="com.startsoft.musicalsound.MainActivity" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.3" 
     android:background="@drawable/img_bg_header" 
     android:orientation="horizontal" > 

     <ImageButton 
      android:id="@+id/btn_menu" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_gravity="right" 
      android:background="@android:color/transparent" 
      android:src="@drawable/ic_action_sort_by_size" /> 
    </LinearLayout> 
+1

選中此http://stackoverflow.com/questions/6992804/how-to-right-align-widget-in -horizo​​ntal-linear-layout-android – Raghavendra

+0

是否需要佈局嵌套?你爲什麼不使用'RelativeLayout'? – Lamorak

+1

謝謝.Raghavendra – HuynhHan

回答

1

ImageButton的父佈局使用android:gravity="right"。您也可以從中刪除android:layout_gravity="right"

+0

它完美地工作。謝謝你,Lamorak! – HuynhHan

+0

不客氣! – Lamorak

0

設置按鈕的重心向右這樣的: 機器人:layout_gravity =「右」