Android工具栏和Webview



我有一个Webview和一个Toolbar。如果没有工具栏,Web视图是完全可滚动的,但当我添加工具栏时,Web视图将不再滚动。我试过NestedScrollView,但它就是不起作用。以下是我的活动布局:

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100">
<ImageButton
android:id="@+id/homeButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:contentDescription="@string/home"
app:srcCompat="@drawable/ic_home" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:gravity="center"
android:text="@string/app_name"
android:textSize="24sp"
android:textStyle="bold"
app:fontFamily="Sans Serif" />
<ImageButton
android:id="@+id/menuButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:contentDescription="@string/menu"
app:srcCompat="@drawable/ic_menu_black_24dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MailActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

工具栏的Java代码只需创建菜单和主页按钮,而web视图只需加载一个url(启用了javascript(。非常感谢你的帮助!

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100">
<ImageButton
android:id="@+id/homeButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:contentDescription="@string/home"
app:srcCompat="@drawable/ic_home" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:gravity="center"
android:text="@string/app_name"
android:textSize="24sp"
android:textStyle="bold"
app:fontFamily="Sans Serif" />
<ImageButton
android:id="@+id/menuButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:contentDescription="@string/menu"
app:srcCompat="@drawable/ic_menu_black_24dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

您必须将相对布局移动到Appbar之外。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100">
<ImageButton
android:id="@+id/homeButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:contentDescription="@string/home"
app:srcCompat="@drawable/ic_home" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:gravity="center"
android:text="@string/app_name"
android:textSize="24sp"
android:textStyle="bold"
app:fontFamily="Sans Serif" />
<ImageButton
android:id="@+id/menuButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:contentDescription="@string/menu"
app:srcCompat="@drawable/ic_menu_black_24dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>

最新更新