gpt4 book ai didi

Android:带有 match_parent 的布局应占用剩余空间或共享

转载 作者:行者123 更新时间:2023-11-30 01:53:00 25 4
gpt4 key购买 nike

我有一个水平线性布局,包含两个布局。我希望 right 布局与内容 (wrap_content) 一样宽。 left 布局应填充剩余空间。

我尝试在左侧布局(相对布局)上使用“match_parent”,在右侧布局(线性布局)上使用“wrap_content”,但左侧布局占据了所有空间。

我怎样才能解决这个问题,左边的布局只占用空间,剩下的不是全部。就像让正确的布局先占空间一样。

编辑:抱歉,我想张贴图片,但不能,这是代码:

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

左侧的相对布局占据了所有空间(屏幕变为绿色)。我希望相对布局采用线性布局的宽度,这样您就可以在布局中看到按钮。

最佳答案

Android 按照 View 在布局文件中出现的顺序布置 View 。因此,您的第一个 View 会在第二个 View 有机会占用任何空间之前填满所有可用空间。一种解决方法是让您的根布局成为 RelativeLayout 而不是 LinearLayout,让您首先放置右侧 View 。另一种是保留根 LinearLayout 并使用 layout_weight 属性。在您的第一个 View 中,而不是 android:layout_width="match_parent" , 尝试 android:layout_width="0dp"android:layout_weight="1"

关于Android:带有 match_parent 的布局应占用剩余空间或共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32723143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com