У меня есть полноэкранный DialogFragment с панелью инструментов. В правой части панели инструментов у меня есть элемент. Я хочу выполнить действие при нажатии на элемент. Проблема в том, что когда я нажимаю на нее, ничего не происходит. Но если я нажму рядом с ним на краю экрана, действие будет вызвано.
Я печатаю "Элемент нажат!" когда его зовут. Вы можете увидеть панель инструментов ниже.
Метод onMenuItemClick вызывается только тогда, когда я нажимаю на обведенную красным часть экрана.
Это класс, который расширяет DialogFragment:
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setStyle(DialogFragment.STYLE_NORMAL, R.style.Preference_DialogPreference_Material);
fish = getArguments().getParcelable("species");
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fish_details, container, false);
Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
System.out.println("clicked");
return true;
}
});
toolbar.inflateMenu(R.menu.details);
toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setText(R.string.learn);
Меню, которое я использую:
<menu 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"
tools:context="ch.boas.aquatis.android.activity.MainActivity" >
<item android:id="@+id/action_share"
android:title="Share"
android:icon="@drawable/ic_share_species"
app:showAsAction="always"/>
</menu>
Схема этого фрагмента:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/toolbar_title"
style="@style/pages_titles"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/mr_white"
android:gravity="center">
<ImageView
android:id="@+id/fish_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="7dp" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true"
android:indeterminateTint="@color/colorPrimary"
android:indeterminateTintMode="src_atop" />
</RelativeLayout>
</LinearLayout>
<ScrollView style="@style/scrollview_style">
<RelativeLayout
android:id="@+id/abc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingBottom="10dp"
android:paddingTop="190dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button"
android:paddingBottom="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="20dp">
<ImageView
android:id="@+id/logo_iucn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_cr" />
<TextView
android:id="@+id/code_text"
style="@style/ParagraphCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:id="@+id/picto_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:clickable="true"
android:src="@drawable/ic_infobulle" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_share_species" />
<!--<Button
android:id="@+id/shar_fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/share_button"
android:text="share" />-->
</RelativeLayout>
<TextView
android:id="@+id/name"
style="@style/TitleLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-20dp"
android:text="" />
<TextView
android:id="@+id/latin_name"
style="@style/TitleLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text=""
android:textStyle="italic" />
<TextView
android:id="@+id/type"
style="@style/details"
android:text="" />
<TextView
android:id="@+id/continent"
style="@style/details"
android:layout_width="wrap_content"
android:text="" />
<TextView
android:id="@+id/family"
style="@style/details"
android:text=" " />
<TextView
android:id="@+id/features"
style="@style/details"
android:text="" />
<TextView
android:id="@+id/size"
style="@style/details"
android:text="" />
<LinearLayout
android:id="@+id/section_desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
style="@style/view_style"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" />
<!--<TextView
style="@style/TitleLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/description" />-->
<TextView
android:id="@+id/desc"
style="@style/details"
android:text="" />
</LinearLayout>
<LinearLayout
android:id="@+id/section_alert"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
style="@style/view_style"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" />
<TextView
style="@style/TitleLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/alert" />
<TextView
android:id="@+id/alert"
style="@style/details"
android:text="" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>