如何捕捉导航图标从片段上单击工具栏? [英] How to catch navigation icon click on toolbar from fragment?
本文介绍了如何捕捉导航图标从片段上单击工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对话框片段,其中的工具栏布局.我想使后退按钮(导航图标)在工具栏中工作,并在单击时退出片段.但是我无法捕捉(对话框)片段中工具栏导航图标上的click事件.
I have a dialog fragment in which I have toolbar in layout. I want to make back button(Navigation Icon) working in toolbar and exit the fragment when clicked. But I am unable to catch the click event on the toolbar's navigation icon in the (dialog)fragment.
这是我获取工具栏的方式:
Here is how I am getting toolbar :
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setTitle(itemType);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
这是我的对话框片段的布局文件:
Here is my layout file for the dialog fragment :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/panel_cyan"
android:id="@+id/rootLayout"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/color_primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listViewItems"
/>
</RelativeLayout>
**这是迄今为止已尝试但失败的****
**Here is what is have tried so far but failed **
选项项在ID R.id.home中单击
Options item click in id R.id.home
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
工具栏上的
setNavigationOnClick():
setNavigationOnClick() on the toolbar :
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Back clicked!", Toast.LENGTH_SHORT).show();
}
});
推荐答案
在 setSupportActionBar(toolbar)
这篇关于如何捕捉导航图标从片段上单击工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文