android布局框溢出

时间:2013-07-27 15:10:57

标签: android

我有这段代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape"
android:gravity="top" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="@string/hello_world" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="@string/hello_world" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="@string/hello_world" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="@string/hello_world" />
    </RelativeLayout>

</LinearLayout>

有4组图像和文字,但如果我以小分辨率打开它,我只能看到3个盒子。如何将它们设置为在下一行显示?

编辑:它应该如下例

2 个答案:

答案 0 :(得分:0)

您为每个元素设置了恒定宽度(100dp)。它不适合宽度低于400dp的屏幕。解决此问题的一种方法是使用水平滚动视图。

用以下内容替换顶部RelativeLayout。

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

如果您希望在不同的屏幕上使用不同的布局,例如在附图中,只需使用两个不同的布局xml,如here所述。

答案 1 :(得分:0)

看起来您可能正在尝试复制GridView的行为。您可以使用GridView根据视图的大小完成从2列到4列的切换。

根据您的目标版本,另一个可能适用的替代方法是指定在屏幕空间较少时使用的不同布局文件(检查表2“smallestWidth”行):Providing Alternative Resources。您可以通过指定诸如“res / layout-sw600dp /”之类的文件夹并在该文件夹中放置具有相同名称的布局文件来使用此方法。在每个布局文件中,然后使用所需的列数显式布局文件。

如果您的目标是无法识别“sw”资源修改器的设备,您可以在代码中手动模拟此操作,方法是指定要按名称扩充的布局文件,然后根据屏幕选择更大或更小的布局文件大小(当然,当应用程序运行时,您可以使用它。)

如果您想详细说明具体方法,请告诉我。