0

我有两种布局。实际上 5、3 个垂直一个在另一个下方,在中间一个我有 2 个并排的布局。我需要它们固定大小,所以它们确实会改变。但只要我在其中一个按钮上放置一个按钮,它们就会改变大小,如下图所示。如何让他们不改变?无论如何,这是我的代码和图片。

布局一 布局二

<?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:weightSum="20" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="15"
        android:orientation="horizontal" >

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

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

    </LinearLayout>

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

</LinearLayout>
4

2 回答 2

1

尝试将两个内部布局更改为:

选项#1:这将水平固定布局,而不是垂直

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="50" >
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="50" >
 </LinearLayout>

选项#2:这将修复两个方向的布局

LinearLayouts使用嵌套权重非常低效。相反,将您的根视图更改为 aRelativeLayout并摆脱权重。请注意,这不是一个 100% 完整的解决方案,但它肯定会让您入门。这会将第一个LinearLayout放在布局的顶部,最后一个LinearLayout放在布局的底部。中间的将始终位于顶视图下方和底视图下方。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical">
    </LinearLayout>

    <LinearLayout android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top"
        android:layout_above="@+id/bottom"
        android:orientation="horizontal" >

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

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

    </LinearLayout>

    <LinearLayout android:id="@+id/bottom"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>
</RelativeLayout>
于 2013-03-18T13:43:42.790 回答
0

尝试将 layout_weight 更改为中间线性布局的两个子线性布局的 0.5。

于 2013-03-18T14:08:46.473 回答