安卓沉浸式状态栏


最近看到安卓沉浸式状态栏在各大App都在使用,所以特意摸索了一下这个问题,总结如下我的步骤如下:


1:style文件中定义样式:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/material_blue_500</item>
        <!-- 状态栏颜色 -->
        <item name="colorPrimaryDark">@color/material_blue_700</item>
        <!-- 字体颜色 -->
        <item name="colorAccent">@color/material_blue_500</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

</resources>

2:写一个BaseActivity

public class BaseActivity extends AppCompatActivity {

    public float Width;
    // 屏幕高度
    public float Height;

    private int statusBarHeight;
    private int titleBarHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics metrics = new DisplayMetrics();
        Width = metrics.widthPixels;
        Height = metrics.heightPixels;
        setToolbar(0xff009688);
        Rect frame = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        statusBarHeight = frame.top;
        int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
        //statusBarHeight是上面所求的状态栏的高度
        titleBarHeight = contentTop - statusBarHeight;

    }

    protected void setToolbar(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        // enable status bar tint
        tintManager.setStatusBarTintEnabled(true);
        // enable navigation bar tint
        tintManager.setNavigationBarTintEnabled(true);
        //        int color = Color.argb(255, Color.red(255), Color.green(255), Color.blue(255));
        tintManager.setTintColor(color);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 添加头部
     */
    protected void AddToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        toolbar.setLogo(R.mipmap.ic_launcher);
        setSupportActionBar(toolbar);
    }

}

3:在相应的activity中引用:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.activity.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/id_toolbar"
        android:background="@color/material_blue_500"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>

    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.activity.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/id_toolbar"
        android:background="@color/material_blue_500"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>

    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

4:整体总结如下: 当我们安卓正常操作下来会发现状态栏和toolbar会挤在一起,布局有点错乱:

记得如下属性的设置!!!

  • ToolBar高度设置为wrap_content 
  • ToolBar添加属性android:fitsSystemWindows="true"
  • 在style的样式文件中添加如下属性
    <item name="android:windowTranslucentStatus">true</item>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值