我创建了一个网站启动的活动。在应用程序中,我已将 AppTheme 设置为 Theme.AppCompat.NoActionBar,因此操作栏不会显示在任何活动布局上。
这是 webview 布局:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
网页加载正常。但是没有导航属性,我也希望能够“取消”WebView 并返回到父活动。
我试图实现我在 android 开发页面上找到的一些代码,特别是让网页在 WebView 中打开,而不是在浏览器中打开,例如。铬合金。
https://developer.android.com/guide/webapps/webview.html
活动;
public class WebActivity extends AppCompatActivity {
String webAddress;
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
myWebView = (WebView) findViewById(R.id.webview);
webAddress = getIntent().getStringExtra("WEB_ADDRESS");
myWebView.loadUrl(webAddress);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
但是我看不到任何导航属性,我也不确定如何取消活动,因为我已将应用栏作为主题删除。
你应该有工具栏,因为 webview 不提供任何导航按钮。你可以在你的布局中有这样的东西,并用onBackPressed
. 您需要覆盖 onBackPressed 并检查您的 webview canGoBack()
.
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:contentInsetStart="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/backBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_arrow_back_white_24dp"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/backBtn"
android:text="Your title"
android:textSize="22sp"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:ellipsize="end" />
</RelativeLayout>
</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.support.design.widget.CoordinatorLayout>
或者,您也可以使用 chrome 自定义选项卡(可用于 Jellybean 以后)。实现非常简单。Chrome 自定义标签的实现指南请参考这里
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句