【问题标题】:android Webview is consuming whole layoutandroid Webview 正在消耗整个布局
【发布时间】:2016-06-30 23:32:04
【问题描述】:

在我的项目中,我有以下 layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
    <LinearLayout
        android:background="#ffffff"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageButton
                android:id="@+id/button"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="left"
                android:layout_margin="4dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@android:color/white"
                android:text="@string/textview"
                android:textSize="30sp"
                android:id="@+id/textview"/>

            <ImageButton
                android:id="@+id/button2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="4dp"
                android:layout_gravity="right"/>
        </FrameLayout>

        <LinearLayout
            android:background="#ffffff"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scrollbars="none" />

            <WebView
                android:id="@+id/webview"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:background="@drawable/border">
            </WebView>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textview2"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="@string/textview2"
                android:textSize="28sp" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

所以我在主 FrameLayout 中有三 (3) 个子布局

-FrameLayout
   -LinearLayout
       -İmageButton(1)
       -Textview(1)
       -İmageButton(2)
   -LinearLayout
       -Webview
   -LinearLAyout
       -textview(2)

但不幸的是 android 错误地渲染了布局

主要也是唯一的问题是:textview(2) 没有显示在屏幕上

这是页面截图:

1 ---> button
2 ---> textview(1)
3 ---> button2
4 ---> webview
5 ---> textview(2) //XXX main point

但我想要以下内容:

我该怎么做?

我需要做哪些修改?

提前感谢

【问题讨论】:

  • 不要使用 fill_parent 作为父线性布局的高度,而是使用权重:developer.android.com/guide/topics/ui/layout/linear.html#Weight
  • 那么我应该将 layout.height 更改为 layout.weight=1 还是 0? webview的Linearlayout对吗?
  • 使用匹配父级并将 webview 设置在您的第 5 个无布局之上。

标签: android android-layout layout webview


【解决方案1】:

您可以使用权重属性而不是填充父项

使用下面的代码,我已经修改了

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
    <LinearLayout
        android:background="#ffffff"
        android:orientation="vertical"
        android:weightSum="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_weight="0.1"
        android:layout_height="0dp">

        <ImageButton
            android:id="@+id/button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@color/blackColor"
            android:layout_gravity="left"
            android:layout_margin="4dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@android:color/white"
            android:text="@string/textview"
            android:textSize="30sp"
            android:id="@+id/textview"/>

        <ImageButton
            android:id="@+id/button2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@color/blackColor"
            android:layout_margin="4dp"
            android:layout_gravity="right"/>
    </FrameLayout>

    <LinearLayout
        android:background="#ffffff"
        android:orientation="horizontal"
        android:layout_weight="0.8"
        android:layout_width="fill_parent"
        android:layout_height="0dp">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scrollbars="none" />

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:background="@drawable/border">
        </WebView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="0.1"
        android:layout_height="0dp">

        <TextView
            android:id="@+id/textview2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="@string/textview2"
            android:textSize="28sp" />
    </LinearLayout>
</LinearLayout>
</FrameLayout>

您可以使用android:layout_weight="0.1" 属性并进行适当的更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多