如何将元素定位在 LinearLayout 中的 WebView 下方?

huangapple go评论64阅读模式
英文:

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.

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/blue&quot;
        android:text=&quot;test1&quot;/&gt;

    &lt;WebView
        android:id=&quot;@+id/webview&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot; /&gt;

    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/blue&quot;
        android:text=&quot;test2&quot;/&gt;
&lt;/LinearLayout&gt;

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:

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    android:weightSum=&quot;2&quot;&gt;

    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/blue&quot;
        android:text=&quot;test1&quot;
        android:layout_weight=&quot;1&quot;/&gt;

    &lt;WebView
        android:id=&quot;@+id/webview&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_weight=&quot;0&quot; /&gt;

    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/blue&quot;
        android:text=&quot;test2&quot;
        android:layout_weight=&quot;1&quot;/&gt;
&lt;/LinearLayout&gt;

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:

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    android:weightSum=&quot;2.1&quot;&gt;

    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/blue&quot;
        android:text=&quot;test1&quot;
        android:layout_weight=&quot;1&quot;/&gt;

    &lt;WebView
        android:id=&quot;@+id/webview&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_weight=&quot;0.1&quot; /&gt;

    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/blue&quot;
        android:text=&quot;test2&quot;
        android:layout_weight=&quot;1&quot;/&gt;
&lt;/LinearLayout&gt;

答案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=&quot;0dp&quot; and android:layout_weight=&quot;1&quot; so that your webview takes up all the free space between the buttons in height:

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/blue&quot;
        android:text=&quot;test1&quot;/&gt;

    &lt;WebView
        android:id=&quot;@+id/webview&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_weight=&quot;1&quot;/&gt;

    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/blue&quot;
        android:text=&quot;test2&quot;/&gt;
&lt;/LinearLayout&gt;

huangapple
  • 本文由 发表于 2023年5月15日 03:08:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定