Android:对齐两张图片

时间:2014-03-21 10:09:02

标签: android imageview alignment

我正在尝试将2 Imageview放入LinearLayout,但我与之并不匹配。我希望它们出现:

|< ---- SPACE ----> IMAGE 1 < ---- SPACE ----> IMAGE 2 < ---- ---- SPACE> |

但左边的2张图片没有边距。这是我的代码:

JAVA

    int tercioPantalla = Modulo.anchoPantalla(this) / 3;

    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(tercioPantalla, tercioPantalla / 2);
    layoutParams2.gravity = Gravity.CENTER;
    ruta1.setLayoutParams(layoutParams2);

    LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(tercioPantalla, tercioPantalla / 2);
    layoutParams3.gravity = Gravity.CENTER;
    ruta2.setLayoutParams(layoutParams3);

Modulo.anchoPantalla

public static int anchoPantalla(Context context) {

    int ancho = 0;

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    ancho = (int) (metrics.widthPixels);

    return ancho;
}

XML

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/bus_ruta1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ruta1_selector" />

            <ImageView
                android:id="@+id/bus_ruta2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ruta2_selector" />
        </LinearLayout>

3 个答案:

答案 0 :(得分:3)

试试这个..

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/bus_ruta1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:src="@drawable/ruta1_selector" />

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

修改

LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);

ruta1.setLayoutParams(lp_icon);
ruta2.setLayoutParams(lp_icon);

答案 1 :(得分:0)

您刚刚见过您LinearLayout,我认为您可以使用android:layout_weight并为每个ImageView赋予同等权重android:layout_weight="1",希望能解决问题。

答案 2 :(得分:0)

只需使用重量,马丁参数来自定义您的布局:

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" >

                <ImageView
                    android:id="@+id/status_left_arrow"
                    android:layout_width="30dp"
                    android:layout_height="14dp"
                    android:layout_gravity="center|bottom"
                    android:layout_weight="1"
                    android:scaleType="fitCenter"
                    android:src="@drawable/status_arrow" />

                <ImageView
                    android:id="@+id/status_center_arrow"
                    android:layout_width="30dp"
                    android:layout_height="14dp"
                    android:layout_gravity="bottom"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:scaleType="fitCenter"
                    android:src="@drawable/status_arrow" />

                <ImageView
                    android:id="@+id/status_right_arrow"
                    android:layout_width="30dp"
                    android:layout_height="14dp"
                    android:layout_gravity="bottom"
                    android:layout_weight="1"
                    android:scaleType="fitCenter"
                    android:src="@drawable/status_arrow" />
            </LinearLayout>