【问题标题】:Remove gap at the top of fragment when actionBar is hidden隐藏actionBar时删除片段顶部的间隙
【发布时间】:2016-06-25 02:11:38
【问题描述】:

当我执行 actionBar.hide() 时,我遇到了在片段顶部显示间隙的问题(顺便说一句,我在模拟器上看不到这个间隙,仅在真实设备上)。我尝试了this answerthat one,但没有任何效果。这个片段在 MainActivity 里面,当我点击菜单时它会加载不同的片段。这是唯一应该隐藏 actionBar(自定义)的片段,因此我无法在 MainActivity onCreate() 方法上设置全屏标志。如何重绘视图以使 actionBar 空白空间消失?

这是现在的样子:

这是我加载片段的 mainActivty:

public void onCreate(Bundle savedInstanceState) {

        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        super.onCreate(savedInstanceState);

        showActionBar();

        if(Build.VERSION.SDK_INT >= 21){
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        setContentView(R.layout.activity_main);
        setUpMenu();
}

点击菜单后,我相应地更改了片段:

@Override
    public void onClick(View view) {
        if (view == itemProfile){
            mActionBar.hide();
            ProfileFragment profile = new ProfileFragment();
            profile.setUser(AppController.getInstance().getLoggedInUser());
            changeFragment(profile);
        }
}

还有自定义的actionBar

private void showActionBar() {
        mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ffffff"));
        mActionBar.setBackgroundDrawable(colorDrawable);
        LayoutInflater mInflater = LayoutInflater.from(this);

        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
        mLogo = (ImageView) mCustomView.findViewById(R.id.logo);

        ImageButton menuButton = (ImageButton) mCustomView
                .findViewById(R.id.menu);
        menuButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (resideMenu.isOpened()) {
                    resideMenu.closeMenu();
                } else {
                    resideMenu.openMenu();
                }
            }
        });

        ImageButton mapButton = (ImageButton) mCustomView
                .findViewById(R.id.map);
        mapButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), MapsActivity.class);
                startActivity(i);
            }
        });

        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

    }

我的主题:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:colorPrimaryDark">@android:color/black</item>
    <item name="android:windowActionBarOverlay">true</item>
</style>

我的 mainActivity 布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:id="@+id/main_fragment">
            </FrameLayout>

        </LinearLayout>
    </FrameLayout>
</FrameLayout>

我的自定义操作栏

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transparent">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent">

        <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="7dp"
        android:fontFamily="sans-serif-light"
        android:text="@string/app_title"
        android:textSize="@dimen/textsize_large"
        android:textColor="@android:color/black"
        android:layout_gravity="center"
        android:visibility="gone"/>

        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="7dp"
            android:layout_gravity="center"
            android:src="@drawable/logo"
            android:background="@color/transparent" />

        <ImageButton android:src="@drawable/earth"
            android:background="?attr/actionBarItemBackground"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="20dp"
            android:scaleType="centerInside"
            android:id="@+id/map"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="right|center_vertical"
            android:layout_marginRight="-15dp"
            android:tint="@color/black"/>

        <ImageButton android:src="@drawable/titlebar_menu_selector"
            android:background="?attr/actionBarItemBackground"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="match_parent"
            android:scaleType="centerInside"
            android:id="@+id/menu"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="-15dp"
            android:layout_gravity="left|center_vertical"
            android:tint="@color/black"/>

    </FrameLayout>

</RelativeLayout>

任何帮助都会很棒,我已经与它斗争了好几天了,我真的不知道该怎么办......

【问题讨论】:

    标签: android android-fragments android-actionbar


    【解决方案1】:

    如果您已经创建了底部导航活动并且您不希望隐藏 ActionBar 的空白,请检查主机活动布局 XML 文件, 删除下面提到的行

        android:paddingTop="?attr/actionBarSize"
    

    【讨论】:

    • 最有用的解决方案。
    【解决方案2】:

    替换

    mActionBar.hide();
    

    mActionBar.setVisibility(View.GONE);
    

    您需要将 Toolbar 添加到您的 Activity 布局中。找到下面的代码 sn-p 用于简单的工具栏布局。

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#2196F3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.Toolbar>
    

    将主题应用于活动。在此步骤中,您需要将我们在步骤 1 中创建的主题应用于您的活动。这可以通过在应用程序 AndroidManifest.xml 中使用 android:theme 属性来完成。

    <activity
            android:name="com.javatechig.sample.MyActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    </activity>
    

    现在你几乎准备好了。您只需要实例化 Toolbar 并使用 setSupportActionBar(Toolbar) 方法将其添加到您的活动中。

     import android.support.v7.app.ActionBarActivity;
     import android.support.v7.widget.Toolbar;
    
    public class MyActivity extends ActionBarActivity{
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    
        // Set a toolbar to replace the action bar.
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    }
    

    访问下面的链接了解更多..

    Using Toolbar as ActionBar

    【讨论】:

    • 不幸的是,它没有工作,现在它显示一个空白而不是一个黑色。
    • 你应该使用工具栏而不是操作栏...然后尝试将上面的代码应用到工具栏...
    • 如何使用工具栏而不是操作栏?
    【解决方案3】:

    把这条线放在你的工具栏上帮助我清除差距,不知道它是否适用于其他组件,如你的相对布局。希望有帮助:)

    android:fitsSystemWindows="true"
    

    喜欢这个

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"    //add this line
    android:background="@color/transparent">
    

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多