Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 菜单项单击,显示片段_Android_Android Fragments_Android Activity - Fatal编程技术网

Android 菜单项单击,显示片段

Android 菜单项单击,显示片段,android,android-fragments,android-activity,Android,Android Fragments,Android Activity,我一直在寻找这个,但未能实现它。我的问题是,我在工具栏中有两个菜单动作设置和重复输入,当我单击动作设置时,应该显示片段类别设置,当我单击重复输入时,应该显示片段 这是我的菜单\u main.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://sch

我一直在寻找这个,但未能实现它。我的问题是,我在工具栏中有两个菜单
动作设置
重复输入
,当我单击动作设置时,应该显示片段类别设置,当我单击
重复输入时,应该显示片段

这是我的菜单\u main.xml

<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=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
    <item android:id="@+id/repeat_entry" android:title="Repeat Entry"
        android:orderInCategory="100" app:showAsAction="never" />
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"/>
</menu>
<LinearLayout
            android:id="@+id/category_cont"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <fragment
                android:name="me2.nakame.hp.moneytracer.CategorySettings"
                android:id="@+id/categorySettings"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>
这是我的活动的一部分\u main.xml

<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=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
    <item android:id="@+id/repeat_entry" android:title="Repeat Entry"
        android:orderInCategory="100" app:showAsAction="never" />
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"/>
</menu>
<LinearLayout
            android:id="@+id/category_cont"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <fragment
                android:name="me2.nakame.hp.moneytracer.CategorySettings"
                android:id="@+id/categorySettings"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>

我的问题只是分类设置显示了

我已经解决了我的问题,只需删除
并使用。替换片段事务的方法

这是activity_main.xml的更新部分,在这里我删除了片段

<LinearLayout
            android:id="@+id/category_cont"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

</LinearLayout>

“片段应该显示”这是您想要的行为,现在请提及您面临的问题?你的碎片没有显示出来吗?任何碰撞?试着用替换。@Harry谢谢您的建议,先生
 public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();


            Fragment fragment = null;
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                fragment = new CategorySettings();
            }else if(id == R.id.repeat_entry){
                fragment = new RepeatEntry();

            }

            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.category_cont,fragment);
            ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
            ft.commit();

            return super.onOptionsItemSelected(item);
        }