【发布时间】: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(水平)的高度是其子级的最大高度