android 状态栏设置工具栏,android – 如何使用DrawerLayout显示在ActionBar /工具栏和状态栏下?...

框架和支持libs中的新功能正是这样。有三个“拼图”:

>使用Toolbar,以便您可以将操作栏嵌入到视图层次结构中。

>制作DrawerLayout fitsSystemWindows,使其位于系统栏后面。

>禁用Theme.Material的正常状态栏着色,以便DrawerLayout可以绘制在那里。

我假设你将使用新的appcompat。

首先,你的布局应该看起来像这样:

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/my_drawer_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/my_awesome_toolbar"

android:layout_height="wrap_content"

android:layout_width="match_parent"

android:minHeight="?attr/actionBarSize"

android:background="?attr/colorPrimary" />

android:layout_width="304dp"

android:layout_height="match_parent"

android:layout_gravity="left|start"

android:fitsSystemWindows="true">

然后在您的活动/片段:

public void onCreate(Bundled savedInstanceState) {

super.onCreate(savedInstanceState);

// Your normal setup. Blah blah ...

// As we're using a Toolbar, we should retrieve it and set it

// to be our ActionBar

Toolbar toolbar = (...) findViewById(R.id.my_awesome_toolbar);

setSupportActionBar(toolbar);

// Now retrieve the DrawerLayout so that we can set the status bar color.

// This only takes effect on Lollipop, or when using translucentStatusBar

// on KitKat.

DrawerLayout drawerLayout = (...) findViewById(R.id.my_drawer_layout);

drawerLayout.setStatusBarBackgroundColor(yourChosenColor);

}

然后你需要确保DrawerLayout在状态栏后面可见。你通过改变你的值-v21主题:

values-v21 / themes.xml

true

@android:color/transparent

true

注意:

如果而不是

android:layout_width="304dp"

android:layout_height="match_parent"

android:layout_gravity="left|start"

android:fitsSystemWindows="true">

实际布局,如果你在从onCreateView方法返回的视图上调用fitsSystemWindows(boolean),将实现所需的效果。

@Override

public View onCreateView(LayoutInflater inflater,

ViewGroup container,

Bundle savedInstanceState) {

View mDrawerListView = inflater.inflate(

R.layout.fragment_navigation_drawer, container, false);

mDrawerListView.setFitsSystemWindows(true);

return mDrawerListView;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值