简体   繁体   English

如何制作固定的XML布局?

[英]How to make fixed XML layouts?

I have two layouts. 我有两种布局。 Actually 5, 3 vertical one below another and in the middle one I have 2 layouts side by side. 实际上是5个,三个垂直的一个接一个,中间一个,我有2个并排的布局。 I need them to be fixed size, so they do change. 我需要将它们固定为大小,因此它们确实会发生变化。 But as soon as I place a button i one of them, they change size, as you can see on the picture below. 但是,一旦我在其中一个按钮上放了一个按钮,它们就会改变大小,如下图所示。 How to make them not change? 如何使它们不变? Anyway, here's my code and pictures. 无论如何,这是我的代码和图片。

版式一布局二

<?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>

Try changing the two inner layouts to this: 尝试将两个内部布局更改为此:

Option #1 : This will fix the layouts horizontally, not vertically 选项#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>

Option #2 : This will fix the layouts in both directions 选项2 :这将双向固定布局

It is very inefficient to use LinearLayouts with nested weights. 使用带有嵌套权重的LinearLayouts效率很低。 Instead, change your root view to a RelativeLayout and get rid of the weights. 相反,将您的根视图更改为RelativeLayout并摆脱权重。 Please note that this isn't a 100% complete solution but it will definitely get you started. 请注意,这不是100%完整的解决方案,但绝对可以帮助您入门。 This will put the first LinearLayout at the top of the layout and the last LinearLayout at the bottom of the layout. 这会将第一个LinearLayout放在布局的顶部,最后一个LinearLayout放在布局的底部。 The middle one will always be below the top view and below the bottom view. 中间的将始终位于顶视图下方和底视图下方。

<?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