英文:
How can I position elements below a WebView inside a LinearLayout?
问题
我尝试创建一个带有上方和下方有按钮的WebView片段。上方的按钮显示出来了,但下方的按钮没有显示出来。就好像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="测试1"/>
<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="测试2"/>
</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="测试1"
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="测试2"
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="测试1"
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="测试2"
android:layout_weight="1"/>
</LinearLayout>
英文:
I tried to create a fragment with a WebView which had a Button above and below it. The button above was displayed, but the one below is not shown. It's like the WebView is trying to take up all the rest of the space on the screen, regardless of what other elements might be in the 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>
How can I show things that are underneath the WebView and stop the WebView from occupying all the rest of the space in the view?
When trying to add weights, things got even more weird. Giving the buttons a weight of 1 and the WebView a weight of 0, meant the buttons didn't show at all:
<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>
And then finally when trying to give the WebView a weight of 0.1, the buttons did both show but they had a height of only around 2dp on the screen:
<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>
答案1
得分: 1
Set android:layout_height="0dp" 和 android:layout_weight="1",以便您的 webview 在高度上占据按钮之间的所有空闲空间:
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
英文:
Set android:layout_height="0dp" and android:layout_weight="1" so that your webview takes up all the free space between the buttons in height:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论