Android LinearLayout:高度未正确显示

3

我有一个白色背景的LinearLayout,里面填充了一堆LinearLayouts和RelativeLayouts。我尝试设置父布局的最小高度,但它似乎仍然会根据内容进行包裹。

这是现在的样子。白色空间应该有一个设置的最小高度,大于现在的高度。 enter image description here

以下是我认为相关的xml代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contactViewScrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ABABAB" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dip"
        android:background="#FFFFFF"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/contactHeader"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#EDEDED"
            android:paddingLeft="10dp" >

            <TextView
                android:id="@+id/fullName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="175dip"
                android:paddingTop="5dp"
                android:textSize="18sp" />

            <View
                android:layout_width="1dp"
                android:layout_height="90dp"
                android:layout_toLeftOf="@+id/image"
                android:background="#000000" />

            <ImageView
                android:id="@+id/image"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_alignParentRight="true"
                android:background="#D6D6D6"
                android:contentDescription="@string/contact_image_description"
                android:scaleType="centerCrop" />
        </RelativeLayout>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#000000" />

        <LinearLayout
            android:id="@+id/mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="10dp" >

            <TextView
                android:id="@+id/mobilePhoneHeading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="15dp"
                android:text="@string/mobile_phone"
                android:textColor="#787878" />

            <TextView
                android:id="@+id/mobilePhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp"
                android:paddingTop="2dp"
                android:textSize="18sp" />
        </LinearLayout>

......以及许多其他LinearLayout,所有这些都是通过编程设置为 'GONE'。

有人能提出解决方案吗?我无法弄清为什么白色区域占用的空间如此之少。


快速提示:有两个视图具有相同的“@+id/image”。这也不会有所帮助。 - OhNoNotScott
3个回答

3
// try this
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contactViewScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ABABAB" >

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#FFFFFF"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/contactHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#EDEDED"
                android:paddingLeft="10dp" >

                <TextView
                    android:id="@+id/fullName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="175dp"
                    android:paddingTop="5dp"
                    android:textSize="18sp" />

                <View
                    android:layout_width="1dp"
                    android:layout_height="90dp"
                    android:layout_toLeftOf="@id/image"
                    android:background="#000000" />

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="90dp"
                    android:layout_height="90dp"
                    android:layout_alignParentRight="true"
                    android:background="#D6D6D6"
                    android:contentDescription="contact_image_description"
                    android:scaleType="centerCrop" />
            </RelativeLayout>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#000000" />

            <LinearLayout
                android:id="@+id/mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="10dp" >

                <TextView
                    android:id="@+id/mobilePhoneHeading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="15dp"
                    android:text="mobile_phone"
                    android:textColor="#787878" />

                <TextView
                    android:id="@+id/mobilePhone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingBottom="10dp"
                    android:paddingTop="2dp"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

1

将第二个线性布局的高度设置。现在它被设置为自适应内容(wrap content)..请执行以下操作

    <LinearLayout
        android:id="@+id/mobile"
        android:layout_width="match_parent"
        android:layout_height="50dp"  //for example
        android:orientation="vertical"
        android:paddingLeft="10dp" >

        <TextView
            android:id="@+id/mobilePhoneHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="15dp"
            android:text="@string/mobile_phone"
            android:textColor="#787878" />

        <TextView
            android:id="@+id/mobilePhone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingTop="2dp"
            android:textSize="18sp" />
    </LinearLayout>

谢谢,我现在正在尝试。 - false_azure
似乎没有解决问题,但可能是我代码中的某些问题。 - false_azure

1

可能是因为设备大小的原因。我强烈建议使用线性布局的权重。这样无论是平板还是手机,都可以完美地显示。

如果您需要更多指导或在使用权重时遇到任何问题,请告诉我。


谢谢你的回答,我甚至没有想到使用权重,所以这很好知道。 - false_azure

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接