运行时图像按钮的位置会发生变化

时间:2015-05-13 08:41:16

标签: android android-layout

这是我的XML文件,用于在屏幕上放置5个按钮。当我在Android工作室上放置按钮时,它看起来很不错,但是当我在一个小屏幕尺寸的手机上调试它时,元素的位置不正确,图像按钮相互重叠

<ImageButton
    android:layout_width="wrap_content"
    android:adjustViewBounds="true"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton1"
    android:src="@drawable/btn_01"
    android:layout_marginLeft="65dp"
    android:layout_marginTop="11dp"
    android:layout_below="@+id/txtReceive" />

<ImageButton
    android:layout_width="wrap_content"
    android:adjustViewBounds="true"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton2"
    android:src="@drawable/btn_02"
    android:layout_marginLeft="52dp"
    android:layout_marginTop="50dp"
    android:layout_below="@+id/txtReceive"/>

<ImageButton
    android:layout_width="wrap_content"
    android:adjustViewBounds="true"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton3"
    android:src="@drawable/btn_03"
    android:layout_marginLeft="52dp"
    android:layout_marginTop="92dp"
    android:layout_below="@+id/txtReceive" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:id="@+id/imageButton4"
    android:src="@drawable/btn_04"
    android:layout_marginLeft="62dp"
    android:layout_marginTop="135dp"
    android:layout_below="@+id/txtReceive" />

<ImageView
    android:layout_width="wrap_content"
    android:adjustViewBounds="true"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton5"
    android:src="@drawable/send_btn"
    android:layout_marginLeft="165dp"
    android:layout_marginTop="55dp"
    android:layout_below="@+id/txtReceive" />

1 个答案:

答案 0 :(得分:0)

正如Nikunj Sakhrelia所指出的,ImageButtons在不同的屏幕尺寸上可能会有不同的渲染效果,在设置边距以定位按钮时会遇到麻烦。最好使用LinearLayout,其方向设置为垂直,以确保图像按钮显示在彼此下方,使用边距仅创建空白。