如何从片段中删除操作栏?

时间:2016-02-02 20:12:08

标签: android android-fragments android-actionbar android-coordinatorlayout

主要活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setCustomView(R.layout.action_bar_layout);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        MedSectionFragment medSectionFragment = (MedSectionFragment)getSupportFragmentManager().findFragmentByTag("medSectionFragment");
        if (medSectionFragment == null){
            medSectionFragment = new MedSectionFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(android.R.id.content, medSectionFragment, "medSectionFragment");

            // transaction.addToBackStack(null);
            transaction.commit();
          }
    }

First Fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        //Inflate the fragment layout file
        ViewGroup rootView=(ViewGroup)inflater.inflate(R.layout.med_sections_list, container, false);
}

第二片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        //Inflate the fragment layout file
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.official_name_list, container, false);
}

从一个片段(类别)转换到另一个(子类别)后,我们有了带有操作栏的列表视图。这是真的。 enter image description here

但是当我转换到第三个片段(详细信息)时

public class DetailMedicineFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.detail_medicine_fragment, container, false);
        return rootView;
    }
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.mToolbar);
          ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
      }

发生错误

  

java.lang.IllegalStateException:此Activity已经有一个动作   酒吧提供的窗户装饰。不要求   Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false   您的主题是使用工具栏。                                                                                     在   android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)                                                                                     在   android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)                                                                                     在   ua.edu.sumdu.medmaterialdesign.DetailMedicineFragment.onActivityCreated(DetailMedicineFragment.java:41)                                                                                     在   android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1970)                                                                                     在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)                                                                                     在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)                                                                                     在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)                                                                                     在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)                                                                                     在   android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:517)                                                                                     在android.os.Handler.handleCallback(Handler.java:739)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                     在android.os.Looper.loop(Looper.java:211)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:5389)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在java.lang.reflect.Method.invoke(Method.java:372)                                                                                     在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1020)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

申请样式

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

1 个答案:

答案 0 :(得分:1)

  

此活动已有窗口装饰提供的操作栏。    请勿请求Window.FEATURE_SUPPORT_ACTION_BAR 并设置   主题中 windowActionBar为false以使用工具栏

由于这个问题已经有了答案(以上记录)并且你之前已经这样做了,所以应该用这两个来完成:

<item name="windowActionBar">false</item> 
<item name="windowNoTitle">true</item>

此外,由于您添加了Toolbar,因此请确保Activity的{​​{1}}父级是:

Styles

重点是:<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 。(就像我猜测的那样,你使用NoActionBar

另外:Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

  

<强> onActivityCreated():

     

如名称所示, 在<{em> 之后调用DarkActionBar的{​​{1}}   完成。它在Activity之后调用,主要用于   最终初始化(例如,修改UI元素)。

听起来不错,但是,在onCreate()上使用它会怎么样?

onCreateView()

它应隐藏onCreate

但是,关于这一行:

getActivity().getSupportActionBar().hide()

我认为问题出现在你ActionBar ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar); AppCompatActivity。{/ p>

  

请参阅:   How to get Toolbar from fragment?

     

这个回答:https://stackoverflow.com/a/26998718/4409113

它正试图从那个Fragment投射,而不仅仅是Activity

你说:如何从片段中移除操作栏?

问题是,你为什么试图获得Fragment呢?

请注意,Toolbar没有FragmentsActionbars/Toolbars有。

http://developer.android.com/guide/components/fragments.html