【问题标题】:Android: align two imagesAndroid:对齐两个图像
【发布时间】:2014-04-28 15:24:11
【问题描述】:

我正在尝试将 2 Imageview 放入 LinearLayout,但我无法很好地对齐它。我希望它们出现:

|图像 1图像 2|

但左边的 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>

【问题讨论】:

    标签: android imageview alignment


    【解决方案1】:

    试试这个..

       <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,我写的是同样的东西……另外,他不需要在代码中设置重力,因为它是在布局中完成的。
    • 这是我的旧代码。但我想要宽度和高度以编程方式使用宽度屏幕
    • @Hariharan:不客气。我会给出相同的布局,所以我认为这确实是正确的答案。您只是忘记添加一些边距。
    • @Charlie 很高兴它有帮助。快乐编码
    【解决方案2】:

    您好,刚刚看到您的 LinearLayout ,我认为您可以使用 android:layout_weight 并将每个 ImageViewandroid:layout_weight="1" 同等权重,希望能解决这个问题。

    【讨论】:

    • 这是我的旧代码。但我想要宽度和高度以编程方式使用宽度屏幕
    【解决方案3】:

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

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

    【讨论】:

    • 这是我的旧代码。但我想要宽度和高度以编程方式使用宽度屏幕
    • 你的代码中没有权重参数,试试吧,你的元素会按照屏幕的正确比例替换,你不需要使用任何代码
    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2013-02-28
    • 1970-01-01
    相关资源
    最近更新 更多