【问题标题】:Android LinearLayout static height converting to wrap_contentAndroid LinearLayout 静态高度转换为 wrap_content
【发布时间】:2015-05-28 15:52:21
【问题描述】:

我正在尝试膨胀并显示具有特定高度的 LinearLayout,但显示时高度总是转换为 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="@dimen/dialog_title_height"
    android:padding="@dimen/default_padding">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="Scan for devices" />

    <ProgressBar
        android:id="@+id/progressBarScanning"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

起初它接缝很好,但是当我将 ProgressBar 可见性设置为 View.GONE 时,布局高度缩小到 TextView 的大小,这比父布局上设置的 layout_height 小得多。

但是,如果我将视图包装在具有指定高度的第二个 LinearLayout 中并将父级设置为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="wrap_content"
    android:padding="@dimen/default_padding">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dialog_title_height">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="Scan for devices" />

        <ProgressBar
            android:id="@+id/progressBarScanning"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>

</LinearLayout>

但现在AndroidStudio当然会抱怨第二个LinearLayout没用。

这样做的正确方法是什么?

【问题讨论】:

  • 你可以使用 View.INVISIBLE 作为进度条。
  • 我宁愿完全控制高度而不需要任何魔法
  • 这不是魔法。它是完全控制的。您不想被绘制,但您希望视图占据空间。 INVISIBLE 就是为此目的而设计的
  • 但是它依靠ProgressBar来设置高度但是布局本身应该有高度,不管孩子们
  • LinearLayout(水平)的高度是其子级的最大高度

标签: android android-layout


【解决方案1】:

您可以为特定对象设置权重,而不是线性布局。

<?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="wrap_content"
    android:padding="@dimen/default_padding">

    <TextView
        android:layout_width="0dp"
        android:layout_height="@dimen/dialog_title_height"
        android:gravity="center_vertical"
        android:text="Scan for devices" />

    <ProgressBar
        android:id="@+id/progressBarScanning"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/dialog_title_height" />

</LinearLayout>

希望它会起作用。

【讨论】:

  • 虽然这确实有效,但它接缝有点hacky。另外,如果我从该 TextView 中删除权重并且只有高度 match_parent 它仍然会出现同样的问题
  • View.INVISIBLE 使用 ProgressBar 可见性,而不是 View.GONE
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
相关资源
最近更新 更多