操作栏未完全隐藏

时间:2016-06-04 05:47:43

标签: android android-actionbar

当我从tab2切换到tab1或tab3但是它留下一个空白时,

我想在运行时隐藏我的操作栏

我的活动:(使用TabLayout和3个片段)

enter image description here

切换到另一个标签,现在隐藏了操作栏,但是有一个空格:

enter image description here

我正在使用actionBar.hide / actionBar.show,任何线索如何正确删除这个额外空间???如果我想要实现的目标是不可能的,请指导我采取任何其他方式

代码:

ActionBar actionBar;
........
........
actionBar = getSupportActionBar();
........
........
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

                if (tab == tab2){
                    actionBar.show();


                }else {
                    actionBar.hide();

                }};

添加了Xml代码:

MainActivity包括content_main

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="MainActivity"
    android:animateLayoutChanges="true">

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

content_main:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
android:id="@+id/main_layout"
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
    android:animateLayoutChanges="true">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#e4e4e4"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fillViewport="false"
    android:clickable="false" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tabs"/>

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

尝试使用@来隐藏/显示ActionBar,

requestWindowFeature(Window.FEATURE_ACTION_BAR);

// delaying the hiding of the ActionBar
Handler h = new Handler();
h.post(new Runnable() {     
    @Override
    public void run() {
        getActionBar().hide();//OR
        //getActionBar().show();
    }
});
  

如果活动

getSupportActionBar().hide(); 
or 
getActionBar().hide();
  

如果片段

getActivity().getSupportActionBar().hide();
or
getActivity().getActionbar().hide();

答案 1 :(得分:1)

试试这个

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);

答案 2 :(得分:1)

在我的片段中,这就是我隐藏ActionBar并且它完全正常的方式

// MainActivity is the activity which contains fragment
ActionBar actionBar = ((MainActivity) getActivity()).getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

答案 3 :(得分:0)

xml

<style name="AppTheme.myNoActionBarTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

然后在manifest.xml中

<activity android:name=".NoticeboardActivity" android:theme="@style/AppTheme.myNoActionBarTheme"></activity>