android - 你如何让 View 填充它可用的空间?

标签 android layout

我知道这一定是个简单的问题......我只是不知道如何解决它。

所以(举个例子)

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

相关文章:

iOS:UIView 外的实心边框

java - SWT 按钮和信息栏对齐

java - 如何在改造界面中从 strings.xml 中获取值?

android - 如果您在 iOS 或 Android 上深度链接到您尚未安装的应用程序会怎样?

android - WebView 在 Android 模拟器中不工作

css - 如何让多个样式表在单个页面上工作?

java - 视频不在 WebView 中播放,但播放音频

android - 如何使用循环器创建后台线程

javascript - 使用 jQuery 使 DIV 在屏幕上居中

WPF - DataGrid 列标题对齐