How to show a custom actionbar view from one fragment only

吃可爱长大的小学妹 提交于 2019-12-04 05:23:07

问题


I have a requirement for my app that a custom action bar view be shown from one fragment only (the landing page fragment). The problem is that this action bar is appearing when the user navigates to other fragments. Is there a way to do this without disabling custom view on every fragment?

Thanks


回答1:


For this issue, only show actionbar for one fragment without show on all other fragments, solution could be very easy:

In your activity that hosts your fragments:

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

    // for API >= 11
    getActionBar.hide();
    // for API < 11
    getSupportActionBar().hide();

    ... ...
}

Then in the fragment that you want to show actionbar:

@Override
public void  onAttach(Activity activity){
    activity.getActionBar().show(); //getSupportActionBar() for API<11

}

@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);
    if (hidden) {
       getActivity().getActionBar().hide(); //getSupportActionBar() for API<11
    } else {
       getActivity().getActionBar().show(); //getSupportActionBar() for API<11
}

}




回答2:


Well, I'm just trying to suggest. Why not removing the action bar to all the fragments and just creating a single fragment (the landing page) with a custom action bar thru layout?

To remove ActionBar to all fragments, add this code to tag:

android:theme="@android:style/Theme.Light.NoTitleBar"

Create a Layout with action bar for your landing page through xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >

<RelativeLayout
    android:id="@+id/rlHeaderMain"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:background="#000000"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:padding="5dp" >


</RelativeLayout>



来源:https://stackoverflow.com/questions/28061940/how-to-show-a-custom-actionbar-view-from-one-fragment-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!