代码之家  ›  专栏  ›  技术社区  ›  Merha

工具栏项在DialogFragment中单击

  •  1
  • Merha  · 技术社区  · 8 年前

    我有一个带工具栏的全屏DialogFragment。在工具栏的右侧有一个项目。我想在单击项目时执行操作。问题是,当我点击它时,什么都没有发生。但如果我在屏幕边缘单击它旁边的,则会调用该操作。

    我打印“已单击项目!”当它被调用时。您可以在下面看到工具栏。

    enter image description here

    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>
    
    3 回复  |  直到 8 年前
        1
  •  0
  •   Hemant Parmar RubenP5    8 年前

    像这样应用监听器后,首先在工具栏中膨胀菜单

     Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
     toolbar.inflateMenu(R.menu.details);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
              switch (item.getItemId()) {
            case R.id.action_share:
               //do some here
                    Log.d("debug","action share has clicked");
                return true;
        }
        return false;
    
            }
        });
    
        toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        toolbarTitle.setText(R.string.learn);
    
        2
  •  0
  •   Felhi Abdelhafidh    8 年前

    确保在布局(设计模式)中将项目放置在正确的位置。有时您会使两个元素彼此靠近,但实际上它们是重叠的。

        3
  •  0
  •   Merha    8 年前

    问题在于线性布局。我已经从中排除了AppBarLayout,然后将其放在上面。当它位于LinearLayout中时,Scrollview被放置在该项的上方,因此它的大部分都被隐藏,除了它的右侧(单击的地方)。如果不滚动,我就看不到它。。现在,滚动时,滚动视图会从工具栏下方通过,项目单击工作正常。

    我的布局现在如下所示:

    <android.support.design.widget.AppBarLayout
        android:id="@+id/toolbar_layout"
        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>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar_layout"
        android:orientation="vertical">
    
        <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>