繁体   English   中英

如何制作固定的XML布局?

[英]How to make fixed XML layouts?

我有两种布局。 实际上是5个,三个垂直的一个接一个,中间一个,我有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>

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

选项#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效率很低。 相反,将您的根视图更改为RelativeLayout并摆脱权重。 请注意,这不是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>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM