水平均匀对齐多个图像

时间:2014-02-18 20:47:47

标签: android android-layout

我在线性布局中有2个图像视图,如下所示:

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

如何对齐两个图像,使它们均匀放置在线性布局中。类似于&#34;合理的文字&#34;因此它在图像的左右两侧具有相等的间距和屏幕的边界。

4 个答案:

答案 0 :(得分: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实现它以使其更快。

编辑:我添加了3个属性(启用,可聚焦,可点击)以禁用placehodler视图,以便仅在度量/布局时考虑它们,但在事件处理期间不会考虑它们。

答案 1 :(得分: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>

答案 2 :(得分:0)

对于两个ImageView,您应该可以使用layout_width="0dp"layout_weight="1"scaleType="centerInside"来获得类似的效果。

唯一的区别是图像之间的空间可能更大。

答案 3 :(得分: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>
相关问题