2014-02-18 67 views
0

我有像這樣在一個線性佈局2周圖像的觀點:對齊multipile圖像均勻horizo​​ntaly

<LinearLayout 
    style="@style/home_icon_row" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/ibtn_home_bus" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ico_bus_selector" /> 

    <ImageView 
     android:id="@+id/ibtn_home_butoday" 
     android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
     android:src="@drawable/ico_butoday_selector" /> 
</LinearLayout> 

我怎樣才能對準兩個圖像,以使得它們均勻地放置線性佈局內。類似於「對齊文本」的東西,使得它在圖像的左側和右側與屏幕的邊界具有相等的間距。

+0

不得不承認,我可能會考慮使用「RelativeLayout」。 – trumpetlicks

+0

可能重複:http://stackoverflow.com/questions/3470420/is-it-possible-to-evenly-distribute-buttons-across-the-width-of-an-android-linea – trumpetlicks

+0

你可以使用之前的墊片,你的圖像之間和之後,權重爲1. – njzk2

回答

1
// try this way here is alternative to use two sub linear layout rather three View. 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/home_icon_row" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center"> 
     <ImageView 
      android:id="@+id/ibtn_home_bus" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ico_bus_selector" /> 
     </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center"> 
     <ImageView 
      android:id="@+id/ibtn_home_butoday" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ico_butoday_selector" /> 
    </LinearLayout> 

</LinearLayout> 
+0

啊,這似乎是一個更優雅的解決方案! – Krimson

0

對於這兩種ImageView,您都應該能夠使用layout_width="0dp",layout_weight="1"scaleType="centerInside"獲得類似的效果。

唯一的區別是圖像之間的空間可以更大。

+1

中間的空間將是邊界空間的兩倍 – njzk2

0
<LinearLayout 
    style="@style/home_icon_row" 
    android:orientation="horizontal" 
    android:padding="@dimen/padding"> 

    <ImageView 
     android:id="@+id/ibtn_home_bus" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ico_bus_selector" 
     android:layout_marginRight="@dimen/padding"/> 

    <ImageView 
     android:id="@+id/ibtn_home_butoday" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ico_butoday_selector" /> 
</LinearLayout> 
2

它不是最好的解決辦法,但它應該工作:

<LinearLayout 
style="@style/home_icon_row" 
android:orientation="horizontal"> 

<View 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="0dp" 
    android:enable="false" 
    android:focussable="false" 
    android:clickable="false" 
/> 

<ImageView 
    android:id="@+id/ibtn_home_bus" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ico_bus_selector" /> 

<View 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="0dp" 
    android:enable="false" 
    android:focussable="false" 
    android:clickable="false" /> 

<ImageView 
    android:id="@+id/ibtn_home_butoday" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ico_butoday_selector" /> 

<View 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="0dp" 
    android:enable="false" 
    android:focussable="false" 
    android:clickable="false" /> 
</LinearLayout> 

如果這是某種登陸這是確定的,但如果它是一個很難使用的佈局考慮通過javacode dinamically實現它使其更快。

編輯:我添加了3個屬性(enable,focussable,clickable)來禁用placehodler視圖,以便它們僅在度量/佈局時考慮,而不是在事件處理期間考慮。

+0

真棒!是的,這是一個着陸頁,我在網格樣式中顯示多個圖標。 – Krimson

+0

我不確定這是比這更好的Haresh解決方案。他添加了N個線性佈局,這些是容器,而這增加了N + 1個「空」視圖,可能比容器更輕......? –

+0

耶和這個解決方案你minimazie既java工作和查看hearchy高度,使envets不必穿越其他ViewGroup層。爲了讓它更加明亮,您還可以禁用佔位符視圖,以便事件不會傳遞給他們... –