作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个水平线性布局,包含两个布局。我希望 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/
我是一名优秀的程序员,十分优秀!