简体   繁体   English

Android:具有match_parent的布局应保留剩余空间或共享

[英]Android: layout with match_parent shall take the space left or share

I have a horizontal Linear Layout, containing two Layouts. 我有一个水平的线性布局,其中包含两个布局。 I want the right layout to be as wide as the content (wrap_content). 我希望正确的布局与内容(wrap_content)一样宽。 The left layout shall fill remaining space. 左侧布局应填充剩余空间。

I tried with "match_parent" on the left layout(Relative Layout) and "wrap_content" on the right layout(Linear Layout), but the left layout takes all the space. 我尝试在左侧布局(相对布局)上使用“ match_parent”,在右侧布局(线性布局)上使用“ wrap_content”,但是左侧布局占用了所有空间。

How can I solve this problem, that the left Layout just takes the space, which remains, not everything. 我该如何解决这个问题,即左Layout仅占用剩下的空间,而不是所有空间。 Like letting the right layout take its space first. 就像先让正确的布局占据其空间一样。

EDIT:: Sorry, I wanted to post a picture, but couldnt, here is the code: 编辑::对不起,我想发布一张图片,但是不能,这是代码:

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff27"
    android:layout_gravity="bottom">
</RelativeLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:layout_alignParentStart="true">

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentStart="true" />
</LinearLayout>

The Relative Layout on the left takes ALL the space (the screen becomes green). 左侧的相对布局占用所有空间(屏幕变为绿色)。 I want the Relative Layout to take the width, which remains from the LinearLayout, so you can see the button in the Layout. 我希望相对布局采用从LinearLayout保留的宽度,以便可以在布局中看到按钮。

Android lays out views in the order in which they appear in the layout file. Android会按照视图在布局文件中的显示顺序进行布局。 Therefore, your first view fills up all available space before the second view has a chance to take up any space. 因此,您的第一个视图将占据所有可用空间,然后第二个视图才有机会占用任何空间。 One workaround is to make your root layout a RelativeLayout instead of a LinearLayout, letting you put the right-side view first. 一种解决方法是将根布局设置为RelativeLayout而不是LinearLayout,让您将右侧视图放在第一位。 Another is to leave the root LinearLayout and use the layout_weight attribute. 另一个是保留根LinearLayout并使用layout_weight属性。 On your first view, instead of android:layout_width="match_parent" , try android:layout_width="0dp" and android:layout_weight="1" 在您的第一个视图上,尝试使用android:layout_width="0dp"android:layout_weight="1"代替android:layout_width="match_parent" android:layout_weight="1"

<?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:weightSum="1"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="#00ff27"></RelativeLayout>

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

        <Button
            android:id="@+id/button"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="New Button" />
    </LinearLayout>
</LinearLayout>

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

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