【问题标题】:toolbar item click in DialogFragment在 DialogFragment 中单击工具栏项
【发布时间】:2018-01-19 10:20:38
【问题描述】:

我有一个带有工具栏的全屏 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>

【问题讨论】:

    标签: android toolbar dialogfragment


    【解决方案1】:

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

     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】:

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

    【讨论】:

      【解决方案3】:

      问题出在 LinearLayout 中。我已将 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>
      

      【讨论】:

        猜你喜欢
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-30
        • 1970-01-01
        相关资源
        最近更新 更多