我试图创建一个带有
WebView
的片段,它的上方和下方都有一个 Button
。显示了上面的按钮,但没有显示下面的按钮。这就像 WebView
试图占据屏幕上的所有剩余空间,而不管 LinearLayout
中可能有什么其他元素。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue"
android:text="test1"/>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue"
android:text="test2"/>
</LinearLayout>
如何显示
WebView
下方的内容并阻止 WebView
占据视图中的所有其余空间?
当尝试增加重量时,事情变得更加奇怪。将按钮的权重设置为 1,将
WebView
的权重设置为 0,这意味着按钮根本不显示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue"
android:text="test1"
android:layout_weight="1"/>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue"
android:text="test2"
android:layout_weight="1"/>
</LinearLayout>
最后,当尝试给
WebView
0.1 的权重时,按钮确实都显示了,但它们在屏幕上的高度只有大约 2dp:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2.1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue"
android:text="test1"
android:layout_weight="1"/>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue"
android:text="test2"
android:layout_weight="1"/>
</LinearLayout>
设置
android:layout_height="0dp"
和android:layout_weight="1"
,使您的webview
占据高度按钮之间的所有可用空间:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue"
android:text="test1"/>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/blue"
android:text="test2"/>
</LinearLayout>