2014-02-18 72 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层。为了让它更加明亮,您还可以禁用占位符视图,以便事件不会传递给他们... –