【问题标题】:Android layout_weight for overflowing linear layout heightAndroid layout_weight 用于溢出线性布局高度
【发布时间】:2015-06-07 23:31:41
【问题描述】:

我的场景是这样的: 我有一个线性布局,其中有一个图像视图和大约 10 个编辑文本。我希望 ImageView 是屏幕高度的一半,剩余的编辑文本占据剩余空间并在需要时滚动。

我将线性布局包裹在滚动视图中,并将权重总和 10 分配给线性布局。然后将 5 的 layout_weight 分配给 imageView,其余的则保留 wrap_content 的 layout_height。但这完全隐藏了图像视图。我应该如何获得我想要的布局?

我的 layout.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true"
        >
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
        <ImageView
                android:layout_width="fill_parent"
                android:layout_height="10dp"
                android:id="@+id/imageView" android:layout_gravity="center" android:background="#bcddff"
                />
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Launch Camera"
                android:id="@+id/button" android:layout_gravity="center"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Enter Name"
                android:id="@+id/name" android:layout_gravity="center"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Mobile No."
                android:id="@+id/mobile" android:layout_gravity="center" android:inputType="phone"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Whatsapp No."
                android:id="@+id/whatsapp" android:layout_gravity="center" android:inputType="phone"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Email"
                android:id="@+id/email" android:layout_gravity="center"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Age"
                android:id="@+id/age" android:layout_gravity="center"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Address"
                android:id="@+id/address" android:layout_gravity="center"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="City"
                android:id="@+id/city" android:layout_gravity="center"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="State"
                android:id="@+id/state" android:layout_gravity="center"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Gender(M/F)"
                android:id="@+id/gender" android:layout_gravity="center"/>
        <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Remarks"
                android:id="@+id/remarks" android:layout_gravity="center"/>
    </LinearLayout>
</ScrollView>

【问题讨论】:

标签: android android-layout


【解决方案1】:

当设置布局权重时,高度必须为 match_parent 或 0dp(如果 Match_parent 布局会随着权重的减小而变大,讽刺的是如果为 0dp,则布局会随着权重的增加而变大)。当父级在 wrap_content 中时,布局权重无法准确工作。

希望这会有所帮助,

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >

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

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

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#bcddff" />

    </LinearLayout>

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

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Launch Camera" />

        <EditText
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Name" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/mobile"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Mobile No."
            android:inputType="phone" />

        <EditText
            android:id="@+id/whatsapp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Whatsapp No."
            android:inputType="phone" />

        <EditText
            android:id="@+id/email"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Email" />

        <EditText
            android:id="@+id/age"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Age" />

        <EditText
            android:id="@+id/address"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Address" />

        <EditText
            android:id="@+id/city"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="City" />

        <EditText
            android:id="@+id/state"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="State" />

        <EditText
            android:id="@+id/gender"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Gender(M/F)" />

        <EditText
            android:id="@+id/remarks"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Remarks" />
    </LinearLayout>

</LinearLayout>

</ScrollView>

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 2021-05-03
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2020-11-23
    • 2016-12-11
    相关资源
    最近更新 更多