Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中水平等距显示2行6个项目_Android_Android Layout - Fatal编程技术网

如何在Android中水平等距显示2行6个项目

如何在Android中水平等距显示2行6个项目,android,android-layout,Android,Android Layout,我正在为Android平板电脑编写一个目录应用程序,每页显示6个项目:每行显示3个项目。 每个项目由一个图像(200x300)和一个文本组成。 在Eclipse中使用android xml布局编辑器进行设计似乎一切都很好,因为我为每个项目使用了默认图像和虚拟固定文本:每个项目在水平方向上完全等距 当我在平板电脑上运行时,每个项目都会被从文件系统(200x300)获取的图像覆盖,而文本(可变长度)则是从数据库获取的。 问题是在6个元素中填充了我的自定义数据后,布局似乎不对齐,正如您从该屏幕截图中看

我正在为Android平板电脑编写一个目录应用程序,每页显示6个项目:每行显示3个项目。 每个项目由一个图像(200x300)和一个文本组成。 在Eclipse中使用android xml布局编辑器进行设计似乎一切都很好,因为我为每个项目使用了默认图像和虚拟固定文本:每个项目在水平方向上完全等距

当我在平板电脑上运行时,每个项目都会被从文件系统(200x300)获取的图像覆盖,而文本(可变长度)则是从数据库获取的。 问题是在6个元素中填充了我的自定义数据后,布局似乎不对齐,正如您从该屏幕截图中看到的:

可能(但我不确定)这个问题是由长文本引起的。 是否有办法(如果问题是文本)强制布局保持固定?(或者将字体改小(或者编辑文本,我不在乎))

这是我正在使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RootView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/filaTop"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_margin="15dp"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ivBottle1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/bottle_trans" />

        <TextView
            android:id="@+id/tvBottle1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top|center"
            android:text="vino"
            android:textAlignment="center"
            android:textSize="22sp" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ivBottle2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/bottle_trans" />

        <TextView
            android:id="@+id/tvBottle2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top|center"
            android:text="vino"
            android:textAlignment="center"
            android:textSize="22sp" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ivBottle3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/bottle_trans" />

        <TextView
            android:id="@+id/tvBottle3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top|center"
            android:text="vino"
            android:textAlignment="center"
            android:textSize="22sp" >
        </TextView>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/filaBottom"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_margin="15dp"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ivBottle4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/bottle_trans" />

        <TextView
            android:id="@+id/tvBottle4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top|center"
            android:text="vino"
            android:textAlignment="center"
            android:textSize="22sp" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ivBottle5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/bottle_trans" />

        <TextView
            android:id="@+id/tvBottle5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top|center"
            android:text="vino"
            android:textAlignment="center"
            android:textSize="22sp" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ivBottle6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/bottle_trans" />

        <TextView
            android:id="@+id/tvBottle6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top|center"
            android:text="vino"
            android:textAlignment="center"
            android:textSize="22sp" >
        </TextView>
    </LinearLayout>
</LinearLayout>

具有布局权重时,以0px开始视图。布局权重将使它们都增长到相同的大小。如果你说包装内容,那么它们从不同的大小开始,增长相同的数量,因此它们以不同的大小结束。

如下更新布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
      android:id="@+id/RootView">


    <LinearLayout android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent"
            android:id="@+id/filaTop">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff00ff"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/ivBottle1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:background="@drawable/ic_launcher"
                android:src="@drawable/bottle_trans" />

            <TextView
                android:id="@+id/tvBottle1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
             android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                  android:layout_centerInParent="true"
                android:id="@+id/ivBottle2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:background="@drawable/ic_launcher"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:src="@drawable/bottle_trans" />

            <TextView
                 android:layout_alignParentBottom="true"
                android:id="@+id/tvBottle2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
             android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0000ff"
            android:orientation="vertical" >

            <ImageView
                  android:layout_centerInParent="true"
                android:id="@+id/ivBottle3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                 android:background="@drawable/ic_launcher"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:src="@drawable/bottle_trans" />

            <TextView
                 android:layout_alignParentBottom="true"
                android:id="@+id/tvBottle3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

    </LinearLayout>

    <LinearLayout android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent"
          android:id="@+id/filaBottom">


         <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff00ff"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/ivBottle4"
            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:background="@drawable/ic_launcher"
                android:src="@drawable/bottle_trans" />

            <TextView
                android:id="@+id/tvBottle4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
             android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                  android:layout_centerInParent="true"
                android:id="@+id/ivBottle5"
                 android:layout_width="wrap_content"
                  android:background="@drawable/ic_launcher"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:src="@drawable/bottle_trans" />

            <TextView
                 android:layout_alignParentBottom="true"
                android:id="@+id/tvBottle5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
             android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0000ff"
            android:orientation="vertical" >

            <ImageView
                  android:layout_centerInParent="true"
                android:id="@+id/ivBottle6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                 android:background="@drawable/ic_launcher"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:src="@drawable/bottle_trans" />

            <TextView
                 android:layout_alignParentBottom="true"
                android:id="@+id/tvBottle6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

    </LinearLayout>


</LinearLayout>


同时删除不必要的代码行。

也许您可以试试GridLayout?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
      android:id="@+id/RootView">


    <LinearLayout android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent"
            android:id="@+id/filaTop">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff00ff"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/ivBottle1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:background="@drawable/ic_launcher"
                android:src="@drawable/bottle_trans" />

            <TextView
                android:id="@+id/tvBottle1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
             android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                  android:layout_centerInParent="true"
                android:id="@+id/ivBottle2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:background="@drawable/ic_launcher"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:src="@drawable/bottle_trans" />

            <TextView
                 android:layout_alignParentBottom="true"
                android:id="@+id/tvBottle2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
             android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0000ff"
            android:orientation="vertical" >

            <ImageView
                  android:layout_centerInParent="true"
                android:id="@+id/ivBottle3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                 android:background="@drawable/ic_launcher"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:src="@drawable/bottle_trans" />

            <TextView
                 android:layout_alignParentBottom="true"
                android:id="@+id/tvBottle3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

    </LinearLayout>

    <LinearLayout android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent"
          android:id="@+id/filaBottom">


         <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff00ff"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/ivBottle4"
            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:background="@drawable/ic_launcher"
                android:src="@drawable/bottle_trans" />

            <TextView
                android:id="@+id/tvBottle4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
             android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                  android:layout_centerInParent="true"
                android:id="@+id/ivBottle5"
                 android:layout_width="wrap_content"
                  android:background="@drawable/ic_launcher"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:src="@drawable/bottle_trans" />

            <TextView
                 android:layout_alignParentBottom="true"
                android:id="@+id/tvBottle5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
             android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0000ff"
            android:orientation="vertical" >

            <ImageView
                  android:layout_centerInParent="true"
                android:id="@+id/ivBottle6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                 android:background="@drawable/ic_launcher"
                android:layout_weight="1"
                android:adjustViewBounds="true"
                android:src="@drawable/bottle_trans" />

            <TextView
                 android:layout_alignParentBottom="true"
                android:id="@+id/tvBottle6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:text="vino"
                android:textAlignment="center"
                android:textSize="22sp" >
            </TextView>
        </RelativeLayout>

    </LinearLayout>


</LinearLayout>