我知道这一定是个简单的问题......我只是不知道如何解决它。
所以(举个例子)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button android:id="@+id/fakeButton"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<Button android:id="@+id/saveSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/saveSearch"/>
</LinearLayout>
这行不通 - 第一个按钮会使第二个按钮不可见。权重会使这一切成为一个百分比游戏,这不是我想要的。
我是不是太胖了?
编辑: 我不知道,但顺序似乎很重要(来自答案)。加载布局是迭代的而不是整体的。您可以将第一个元素设置为固定高度,其余元素将填充剩下的元素。但我需要的是使最终元素具有固定高度。
最佳答案
您完全可以使用 layout_weight :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button android:id="@+id/fakeButton"
android:layout_height="1"
android:layout_width="match_parent"
android:layout_weight="1.0" />
<Button android:id="@+id/saveSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/saveSearch"/>
</LinearLayout>
这样,底部按钮将具有基本高度,而 fakeButton 将占据所有剩余空间。
layout_height 控制剩余空间在不同 View 之间的分配方式。默认值为 0(不占用任何额外空间)。如果将两个按钮设置为相同的高度和 1.0 的权重,则每个按钮将占用 50% 的空间。
关于android - 你如何让 View 填充它可用的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6744840/