Immersive status bar - high imitation QQ

Foreword:
Before entering today's main topic, let's talk about my feelings as usual. Recently, I feel that I have lost my sense of direction and I am so confused! Failed to find a job again, is Android really saturated? I haven't gone out for the past two days. Except for the takeaway when I go downstairs, it's the dormitory. After thinking about it for a long time, I still can't forget my original intention. I believe that there are many reasons why I can't find a job. The most important thing is to be skilled enough. That's it, let's run, boy! Next, I will introduce to you how to quickly create an immersive status bar. Although it feels a bit late, it is not finished!


One: What is the immersive status bar?


The immersive status bar is a set of transparent system ui styles provided by Google starting from Android 4.4 to our developers. This style is for the status bar and navigation bar, so that you don't have to face the dark every day like before. There are two black bars on the top and bottom, and it can be adjusted to the same style as Activity to form a complete theme, which is the same as the system above IOS7.0.

Let me first compare the style renderings with and without immersion, as shown in the following figure:


 (non-immersive)



 (immersive)




I believe that everyone knows what an immersive status bar is; at present, opening many apps will have this effect. It is conceivable that the immersive status bar is quite practical!





2. Use the immersive status bar to imitate QQ:


The steps to realize the immersive status bar are very simple: because the immersive status bar was launched after Android 4.4, first add a judgment to the program, that is: when the system version is 4.4 or above, the immersive status can be used bar, and then set the status bar and navigation bar to be transparent in the statement:

// When the system version is 4.4 or above, the immersive status bar can be used
 if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
     // Transparent status bar
 getWindow().addFlags ( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS ) ;
 // Transparent navigation bar
 getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION ) ;
 }            

Finally, add , , , to the layout file

android:fitsSystemWindows="true"
android:clipToPadding="true"

This completes the immersive status bar!
It's that simple, it's so durable! The source code is the best mentor, let's take a look at the overall code:


xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:context=".MainActivity">
                                                                                                  
    <RelativeLayout
        android:fitsSystemWindows="true"
        android:clipToPadding="true"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#0099cc">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/img_head"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="13dp"
            android:layout_marginLeft="15dp"
            app:civ_border_width="2dp"
            app:civ_border_color="#FFFFFF"
            android:src="@mipmap/meinv">
        </de.hdodenhof.circleimageview.CircleImageView>

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="26dp"
            android:layout_centerHorizontal="true"
            android:text="联系人"
            android:textColor="@android:color/white"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_right_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="26dp"
            android:layout_marginRight="15dp"
            android:layout_alignParentRight="true"
            android:text="添加"
            android:textColor="@android:color/white"
            android:textSize="18sp" />
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="沉浸式状态栏"
        android:textSize="22sp"
        android:background="#E0FFFF"/>
    
</LinearLayout>


MainActivity中:

package com.zsml.chaotranstintbar;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        
    }
    
}

实现沉浸式状态栏的其他方法:动态加入、第三方库。

1、动态实现:

动态实现也是比较简单的,首先是隐藏布局,最后动态计算状态栏高度并设置,都是在MainActivity中操作的,布局文件也就不用加上 android:fitsSystemWindows="true"、
  android:clipToPadding="true" 这两句了!

所以直接给源码吧:

MainActivity中:

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import java.lang.reflect.Field;

public class TwoActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_two);

        //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            LinearLayout linear_bar=(LinearLayout)findViewById(R.id.linear_bar);
            linear_bar.setVisibility(View.VISIBLE);
            int statusHeight=getStatusBarHeight();
            LinearLayout.LayoutParams params=(LinearLayout.LayoutParams )linear_bar.getLayoutParams();
            params.height=statusHeight;
            linear_bar.setLayoutParams(params);
        }

    }

    /**
     * 获取状态栏的高度
     * @return
     */
    private int getStatusBarHeight(){
        try
        {
            Class<?> c=Class.forName("com.android.internal.R$dimen");
            Object obj=c.newInstance();
            Field field=c.getField("status_bar_height");
            int x=Integer.parseInt(field.get(obj).toString());
            return  getResources().getDimensionPixelSize(x);
        }catch(Exception e){
            e.printStackTrace();
        }
        return 0;
    }

}

这样就完事了,是不是一样那么简单、、、


2、第三方库实现(SystemBarTint)

SystemBarTint是开源到github上的一个开源库来的;

地址:https://github.com/jgilfelt/SystemBarTint

使用步骤:

关联库:compile'com.readystatesoftware.systembartint:systembartint:1.0.3'

xml布局中添加:

android:fitsSystemWindows="true"
android:clipToPadding="true"

MainActivity中实现:

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.readystatesoftware.systembartint.SystemBarTintManager;

public class ThreeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_three);

        //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            // 激活状态栏
            tintManager.setStatusBarTintEnabled(true);
            // enable navigation bar tint 激活导航栏
            tintManager.setNavigationBarTintEnabled(true);
            //设置系统栏设置颜色
            //tintManager.setTintColor(R.color.red);
            //给状态栏设置颜色
            tintManager.setStatusBarTintResource(R.color.middle_color);
            // 设置导航栏设置资源
            tintManager.setNavigationBarTintResource(R.color.androidColorE);
        }

    }
}

都是大同小异来的,我个人觉得第一种方法是最好实现和理解的,大家都可以尝试一下,希望对你们有所帮助!最后贴出沉浸式状态栏-高仿QQ的效果图如下:




在此就结束了,希望大家多多指教,更多内容请关注:luoweichao.top




尊重原创,转载请注明:From zsml2016(http://blog.csdn.net/qq_29269233)Power byzsml2016侵权必究!






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325563371&siteId=291194637