android状态栏隐藏navigationdrawer
Posted
技术标签:
【中文标题】android状态栏隐藏navigationdrawer【英文标题】:android statusbar hiding navigationdrawer 【发布时间】:2016-05-10 04:22:58 【问题描述】:我有一个带有此代码的导航抽屉:
<?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_ android:layout_
tools:openDrawer="start">
<!--<android.support.design.widget.AppBarLayout android:layout_-->
<!--android:layout_ android:theme="@style/AppTheme.AppBarOverlay">-->
<!--<android.support.v7.widget.Toolbar android:id="@+id/toolbar"-->
<!--android:layout_ android:layout_-->
<!--android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />-->
<!--</android.support.design.widget.AppBarLayout>-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_
android:background="#000000"
android:layout_ >
</FrameLayout>
<!--<android.support.design.widget.AppBarLayout android:layout_-->
<!--android:layout_ android:theme="@style/AppTheme.AppBarOverlay">-->
<!--</android.support.design.widget.AppBarLayout>-->
<!--<include-->
<!--layout="@layout/app_bar_home_page"-->
<!--android:layout_-->
<!--android:layout_ />-->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
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>
但我的问题是操作栏总是在我的导航抽屉上方,隐藏了他的顶部。这是截图:
如您所见,我的操作栏隐藏了导航抽屉。
我该如何解决这个问题?任何帮助将不胜感激。
谢谢大家
PS:
这是我想要实现的,这是我在网上找到的第一张图片。
编辑: 请注意图像,我的目标与“重复”不同。他想将他的列表设置在标题下方,我想用我的抽屉隐藏状态栏
EDIT2:
我回到了原点,现在导航抽屉就在 actionBar 的下方。这是我的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_
android:layout_
tools:openDrawer="start">
<FrameLayout
android:id="@+id/content_frame"
android:layout_
android:background="#000000"
android:layout_ >
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_home_page"
app:menu="@menu/activity_home_page_drawer" />
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
您要隐藏状态栏还是拥有它,并确保它不会覆盖它? 我还想要状态栏,我只是不想让它隐藏我的抽屉 尝试删除,android:fitsSystemWindows="true"
我添加了一个在网上找到的屏幕以获得更好的解释,我会尝试一下
不完全是,他还有一个问题:(
【参考方案1】:
在android.support.v4.widget.DrawerLayout
你可以试试添加
android:paddingTop="?attr/actionBarSize"
这是一个类似的问题Android Navigation Drawer and windowActionBarOverlay = true
或者你可以隐藏操作栏
来自https://***.com/a/22891560/1815624
很简单。
getActionbar().hide(); getActionbar().show();
关于分隔符的最后一部分
<View android:layout_ android:layout_ android:background="@android:color/darker_gray"/>
要添加垂直分隔符,请切换 layout_width 和 layout_height 价值观
或者要将抽屉放在 ActionbBar 的顶部,您可以尝试以下答案:
https://***.com/a/26174941/1815624
以下是您执行此操作的一些详细步骤。
首先,创建一个名为“decor.xml”的 xml 或任何你喜欢的东西。只放 DrawerLayout 和其中的抽屉。下面的“FrameLayout”只是一个 容器。我们将使用它来包装您的活动内容。
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout ...> <FrameLayout android:id="@+id/container" android:orientation="vertical" android:layout_ android:layout_/> <fragment android:name="com...." android:layout_gravity="start" android:id="@id/navigation" android:layout_ android:layout_ /> </android.support.v4.widget.DrawerLayout>
然后删除主布局中的 DrawerLayout。现在布局 你的主要活动应该是这样的
<RelativeLayout android:orientation="vertical" android:layout_ android:layout_> ... </RelativeLayout>
我们假设主活动的布局命名为“main.xml”。
在您的 MainActivity 中,编写如下:
@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
刚刚定义。 container.addView(child);
// Make the drawer replace the first child decor.addView(drawer); // Do what you want to do.......
现在你有了一个可以在 ActionBar 上滑动的 DrawerLayout。但 您可能会发现它被状态栏覆盖。您可能需要添加一个 paddingTop 到 Drawer 以解决这个问题。
这并不理想,但它应该对您有所帮助...白色是抽屉,黑色是主要内容,蓝色是标题栏....
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();
//
【讨论】:
我已经找到了该解决方案,但它不起作用。通过添加它,我遇到了完全相同的问题,您发布的链接是将列表放在“自定义标题”下方。我希望我的整个抽屉都覆盖状态栏 你可以完全隐藏栏 我会投赞成票,但这不是我想要的。这可能是一种解决方法,但我不想完全隐藏我的状态栏,我只想隐藏抽屉下方的部分 今晚或明天我会试试,之后我会发表评论并最终确认这是解决方案。谢谢! 没问题。祝你好运以上是关于android状态栏隐藏navigationdrawer的主要内容,如果未能解决你的问题,请参考以下文章