[英]How do I add back & home button in action bar button for WebView in my android app?
I made an app which uses WebView to display a mobile website. 我制作了一个使用WebView来显示移动网站的应用程序。 I have also added ProgressDialog which appears till the page finishes loading.
我还添加了ProgressDialog,直到页面完成加载为止。 I would like to add a back & home button in the action bar of my app.
我想在我的应用程序的操作栏中添加一个后退和主页按钮。 These buttons should appear when any link is clicked in the home page of the mobile website & disappear when we return to the home page (Similar to Indeed Job search App).
当在移动网站的主页上单击任何链接时,这些按钮应该会出现,而当我们返回主页时,这些按钮应该会消失(类似于Indeed Job search App)。
This page may help you. 此页面可能会对您有所帮助。
Action bar buttons behaves just like the elements of the overflow menu. 操作栏按钮的行为类似于溢出菜单的元素。
Just add one <item>
inside your menu resource file with the attribute app:showAsAction="ifRoom"
or app:showAsAction="always"
. 只需在菜单资源文件中添加一个属性为
app:showAsAction="ifRoom"
或app:showAsAction="always"
<item>
。
According to android dev site your xml should look like this: 根据android开发站点,您的xml应该如下所示:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="@+id/action_back"
android:icon="@drawable/ic_go_back"
android:title="@string/action_back"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>
Then you have to attach a listener for that button: just handle as if it was in the overflow menu. 然后,您必须为该按钮附加一个侦听器:就像在溢出菜单中一样进行处理。 So you have to override
onOptionsItemSelected
adding something like if (mWebView.canGoBack()) {mWebView.goBack();}
因此,您必须重写
onOptionsItemSelected
添加类似if (mWebView.canGoBack()) {mWebView.goBack();}
Here's the code: 这是代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_back:
if (mWebView.canGoBack()) {
mWebView.goBack();
}
// ... other cases ...
}
}
您可以简单地设置这两个菜单操作的可见性,具体取决于URL更改时的当前URL:
actionBack.setVisibility(yourWebView.getUrl().equals("www.yourHomePage.com") ? View.INVISIBLE : View.VISIBLE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.