AppCompatActivity Activity ToolBar 显示设置

来源:互联网 发布:宿迁市网络问政 编辑:程序博客网 时间:2024/06/05 21:03

1、AppCompatActivity是自带actionbar,所以必须先去掉,修改设置activity的theme如下:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">//关键是这个NOACTIONBAR的设置    <!-- Customize your theme here. -->    <item name="colorPrimary">@color/colorPrimary</item>    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>    <item name="colorAccent">@color/colorAccent</item></style>
这样就没有actionbar了。

2、在布局中添加一个toolbar,需要注意的是,有时候toolbar的图标颜色不对,那么需要修改主题,如下是实例代码,这种情况下,图标和文字都是白色的,是统一的。

<android.support.v7.widget.Toolbar    android:id="@+id/toolBar"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@color/colorPrimary"    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
最下面2行是关键。
3、就是代码中设置toolbar为actionbar了。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);toolbar.setTitle("skfksfas");setSupportActionBar(toolbar);if (getSupportActionBar() != null) {    getSupportActionBar().setDisplayHomeAsUpEnabled(true);//这2行是设置返回按钮的    getSupportActionBar().setDisplayShowHomeEnabled(true);}
设置导航也可以用如下方法:
toolbar.setNavigationIcon(R.drawable.back);toolbar.setNavigationOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        finish();    }});
貌似这种方法添加的监听,需要在调用setSupportActionBar之后调用。
2、沉浸式状态栏:
第一步:在activity的主题中,添加属性<item name="android:windowTranslucentStatus">true</item>

第二步:如果你希望view A的某一部分进入到状态栏中,那么对该view设置属性:android:fitsSystemWindows="true".一般这个属性是设置给toolbar.注意:需要给toolbar加一个padding(该padding的高度就是原来statusbar的高度),然后给toolbar设置一个合适的背景颜色就ok了。


阅读全文
0 0
原创粉丝点击