安卓状态栏隐藏导航抽屉

4

我有一个带有以下代码的导航抽屉:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:openDrawer="start">


    <!--<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"-->
        <!--android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">-->

        <!--<android.support.v7.widget.Toolbar android:id="@+id/toolbar"-->
            <!--android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"-->
            <!--android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />-->

    <!--</android.support.design.widget.AppBarLayout>-->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:background="#000000"
        android:layout_height="match_parent" >
    </FrameLayout>


    <!--<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"-->
        <!--android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">-->

    <!--</android.support.design.widget.AppBarLayout>-->

    <!--<include-->
        <!--layout="@layout/app_bar_home_page"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent" />-->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_home_page"
        app:menu="@menu/activity_home_page_drawer"
        app:insetForeground="#4000"/>

</android.support.v4.widget.DrawerLayout>

我也用这个设置了我的样式v21

<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>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="windowActionModeOverlay">true</item>
</style>

但我的问题是,操作栏始终在导航抽屉上方,遮挡其顶部。以下是屏幕截图:

enter image description here

您可以看到我的操作栏遮盖了导航抽屉。

我该怎么解决?任何帮助都将不胜感激。

谢谢大家

PS:

这就是我想要实现的,这是我在网上找到的第一张图片。 enter image description here

编辑: 请注意图片,我的目的与“重复”不同。他想在标题下设置列表,我想用我的抽屉隐藏状态栏。

编辑2:

我回到了起点,现在导航抽屉只是在操作栏下面。这是我的Java代码:

public class HomePageActivity extends AppCompatActivity implements IHomePage {

    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private ActionBarDrawerToggle mDrawerToggle;
    private ActionBar ab;

    private boolean isDetail = false;
    private int lastSelected = 0;

    @Override
    protected void onResume() {
        super.onResume();

        if(mDrawerToggle != null) {
            mDrawerToggle.syncState();
        }

        if(navigationView != null){
            navigationView.getMenu().getItem(0).setChecked(true);
        } else{
            initDrawer();
            navigationView.getMenu().getItem(0).setChecked(true);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        initActionBar() ;
        initDrawer();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        PreferitiFragment fragment = new PreferitiFragment();
        fragmentTransaction.add(R.id.content_frame, fragment);
        fragmentTransaction.commit();

        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    private void initActionBar() {
        ab = getSupportActionBar();
        if (ab == null) {
            Toast.makeText(this, "no", Toast.LENGTH_SHORT).show();
            return;
        }
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeButtonEnabled(true);


    }

    private void initDrawer() {

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                int id = item.getItemId();
                lastSelected = id;

                switch (id){
                    case R.id.nav_favorite:
                        setFragment(new PreferitiFragment());
                        break;

                    case R.id.nav_search:
                        setFragment(new CercaPaeseFragment());
                        break;

                    case R.id.nav_settings:
                        setFragment(new ImpostazioniFragment());
                        break;

                    case R.id.nav_guida:
                        setFragment(new GuidaFragment());
                        break;
                    default:
                        setFragment(new PreferitiFragment());
                        break;
                }

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });

        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                drawerLayout,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

                invalidateOptionsMenu();
//                mDrawerToggle.syncState();
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
//                mDrawerToggle.syncState();
            }


        };
        drawerLayout.setDrawerListener(mDrawerToggle);
    }

    @Override
    public void showDetails(UUID idPharmacy) {

        isDetail = true;
        setFragment(new DettagliFragment());
    }

    @Override
    public void onBackPressed() {
        if (isDetail) {
            if(lastSelected == R.id.nav_search) {
                setFragment(new CercaPaeseFragment());
            } else if(lastSelected == R.id.nav_favorite){
                setFragment(new PreferitiFragment());
            }
        } else {
            super.onBackPressed();
        }
    }

    private void setFragment(Fragment fragment){


        IMyFragments iMyFragments = (IMyFragments) fragment;
        ab.setTitle(iMyFragments.getTitle());

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.content_frame, fragment);
        fragmentTransaction.commit();
    }
}

这是我的主页布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:background="#000000"
        android:layout_height="match_parent" >
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_home_page"
        app:menu="@menu/activity_home_page_drawer" />

</android.support.v4.widget.DrawerLayout>

你想要隐藏状态栏,还是让它显示并确保不会覆盖它? - Anirudh M
我仍然想要状态栏,只是不希望它遮挡我的抽屉。 - Pier Giorgio Misley
2
尝试移除 android:fitsSystemWindows="true" - Anirudh M
我在网上找到了一个屏幕以便更好地解释,我会尝试的。 - Pier Giorgio Misley
不完全是,他有另一个问题 :( - Pier Giorgio Misley
@PierGiorgioMisley,你最终解决了这个问题吗? - CrandellWS
1个回答

3
android.support.v4.widget.DrawerLayout 上,你可以尝试添加。
android:paddingTop="?attr/actionBarSize"

这里有一个类似的问题Android导航抽屉和windowActionBarOverlay = true

或者你可以隐藏操作栏

来自https://dev59.com/UOo6XIcBkEYKwwoYJA7p#22891560

Very simple.

getActionbar().hide();
getActionbar().show();
关于分隔符的最后一部分
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

To add vertical separator, switch the layout_width and layout_height values

或者,如果您想将抽屉放在ActionbBar的顶部,您可以尝试以下答案:

https://dev59.com/1mAg5IYBdhLWcg3ws8ro#26174941

Below is some detailed steps for you to do that.

First, create a xml named "decor.xml" or anything you like. Only put the DrawerLayout and the drawer in. The "FrameLayout" below is just a container. We will use it to wrap your activity's content.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
    <FrameLayout android:id="@+id/container"
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/>
    <fragment android:name="com...." 
        android:layout_gravity="start" 
        android:id="@id/navigation" 
        android:layout_width="@dimen/navigation_menu_width" 
        android:layout_height="fill_parent" />
</android.support.v4.widget.DrawerLayout>

and then remove the DrawerLayout in your main layout. Now the layout of your main activity should look like

<RelativeLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    ...
</RelativeLayout>

we assume that the main activity's layout is named "main.xml".

in your MainActivity, write as the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Inflate the "decor.xml"
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important.

    // HACK: "steal" the first child of decor view
    ViewGroup decor = (ViewGroup) getWindow().getDecorView();
    View child = decor.getChildAt(0);
    decor.removeView(child);
    FrameLayout container = (FrameLayout) drawer.findViewById(R.id.container); // This is the container we

defined just now. container.addView(child);

    // Make the drawer replace the first child
    decor.addView(drawer);

    // Do what you want to do.......

}

Now you've got a DrawerLayout which can slide over the ActionBar. But you might find it covered by status bar. You might need to add a paddingTop to the Drawer in order to fix that.

这不是最理想的,但它应该会帮助你... 白色是抽屉,黑色是主要内容,蓝色是标题栏...

enter image description here

public class HomePageActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private ActionBarDrawerToggle mDrawerToggle;
    private ActionBar ab;

    private boolean isDetail = false;
    private int lastSelected = 0;

    @Override
    protected void onResume() {
        super.onResume();

        if(mDrawerToggle != null) {
            mDrawerToggle.syncState();
        }

//        if(navigationView != null){
//            navigationView.getMenu().getItem(0).setChecked(true);
//        } else{
//            initDrawer();
//            navigationView.getMenu().getItem(0).setChecked(true);
//        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        initActionBar() ;
        initDrawer();

//        FragmentManager fragmentManager = getFragmentManager();
//        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//        PreferitiFragment fragment = new PreferitiFragment();
//        fragmentTransaction.add(R.id.content_frame, fragment);
//        fragmentTransaction.commit();

        mDrawerToggle.syncState();

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.drawer_layout, null);

        // HACK: "steal" the first child of decor view
        ViewGroup decor = (ViewGroup) getWindow().getDecorView();
        View child = decor.getChildAt(0);
        decor.removeView(child);
        FrameLayout container = (FrameLayout) drawer.findViewById(R.id.content_frame); // This is the container we defined just now.
        container.addView(child);
        decor.addView(drawer);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    private void initActionBar() {
        ab = getSupportActionBar();
        if (ab == null) {
            Toast.makeText(this, "no", Toast.LENGTH_SHORT).show();
            return;
        }
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeButtonEnabled(true);


    }

    private void initDrawer() {

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                int id = item.getItemId();
                lastSelected = id;

//                switch (id){
//                    case R.id.nav_favorite:
//                        setFragment(new PreferitiFragment());
//                        break;
//
//                    case R.id.nav_search:
//                        setFragment(new CercaPaeseFragment());
//                        break;
//
//                    case R.id.nav_settings:
//                        setFragment(new ImpostazioniFragment());
//                        break;
//
//                    case R.id.nav_guida:
//                        setFragment(new GuidaFragment());
//                        break;
//                    default:
//                        setFragment(new PreferitiFragment());
//                        break;
//                }

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });

        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                drawerLayout,
                android.R.drawable.ic_btn_speak_now,
                R.string.open,
                R.string.close
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

                invalidateOptionsMenu();
//                mDrawerToggle.syncState();
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
//                mDrawerToggle.syncState();
            }


        };
        drawerLayout.setDrawerListener(mDrawerToggle);
    }

//    @Override
//    public void showDetails(UUID idPharmacy) {
//
//        isDetail = true;
//        setFragment(new DettagliFragment());
//    }

//    @Override
//    public void onBackPressed() {
//        if (isDetail) {
//            if(lastSelected == R.id.nav_search) {
//                setFragment(new CercaPaeseFragment());
//            } else if(lastSelected == R.id.nav_favorite){
//                setFragment(new PreferitiFragment());
//            }
//        } else {
//            super.onBackPressed();
//        }
//    }

//    private void setFragment(Fragment fragment){
//
//
//        IMyFragments iMyFragments = (IMyFragments) fragment;
//        ab.setTitle(iMyFragments.getTitle());
//
//        FragmentManager fragmentManager = getFragmentManager();
//        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//        fragmentTransaction.add(R.id.content_frame, fragment);
//        fragmentTransaction.commit();
//    }
}

我已经找到了那个解决方案,但它并没有起作用。通过添加它,我遇到了完全相同的问题,你发布的链接是关于将列表放在“自定义标题”下面。我希望整个抽屉覆盖状态栏。 - Pier Giorgio Misley
你可以完全隐藏该栏。 - CrandellWS
我会点赞,但这不是我想要的。它可能是一个解决方法,但我不想完全隐藏我的状态栏,我只想隐藏抽屉下面的部分。 - Pier Giorgio Misley
我会在今晚或明天尝试,并在之后发表评论并最终确认它是否为解决方案。谢谢! - Pier Giorgio Misley
没问题。祝你好运。 - CrandellWS
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接