【问题标题】:Action bar is not hiding completely操作栏没有完全隐藏
【发布时间】:2016-10-04 06:06:45
【问题描述】:

当我从 tab2 切换到 tab1 或 tab3 时,我想在运行时隐藏我的操作栏,但它留下了一个空格

我的活动:(带有 TabLayout 和 3 个片段)

切换到另一个选项卡,操作栏现在隐藏但有一个空格:

我正在使用 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 代码:

包含content_main的MainActivity

    <?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>

【问题讨论】:

  • 你能发布你的代码吗?
  • @SathishKumarJ 请检查更新后的问题
  • @DixitPanchal 请检查更新后的问题

标签: android android-actionbar


【解决方案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();

【讨论】:

  • 嘿@Sathish 感谢您的回复,好吧h.post 在这里发布导致错误cannot resolve methodHandler 显示错误Handler is abstract cannot be instantiated 并迫使我实施方法
  • Handler h = new Handler() { @Override public void close() { } @Override public void flush() { } @Override public void publish(LogRecord record) { } }; h.post(new Runnable() { @Override public void run() { getActionBar().hide();//OR //getActionBar().show(); } });
  • 使用导入android.os.Handler;
【解决方案2】:

试试这个

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

【讨论】:

  • 现在它在隐藏操作栏后显示更大的空间:D
【解决方案3】:

在我的片段中,这就是我隐藏ActionBar 的方式,它工作得非常好

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

【讨论】:

  • 兄弟,它隐藏了 actionBar,但仍然留下了不需要的空间
  • 你能把你设置TabLayout的xml贴出来吗?
【解决方案4】:

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>

【讨论】:

    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多