Android 修改状态栏和标题栏颜色

来源:互联网 发布:查看矢量图的软件 编辑:程序博客网 时间:2024/05/22 14:13

一、Android 版本不同 设置状态栏的方式也不相同

在Android 4.4 (kikat)以下版本是无法修改状态栏颜色的。

在Android 4.4(kikat)和5.x(Lollipop)设置的方法也不相同

二、效果图

、代码

/** * 更改状态栏颜色 * * @param activity * @param color */public static void setSimpleStatusBarColor(Activity activity, @ColorInt int color) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);        activity.getWindow().setStatusBarColor(color);    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();        View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);        if (fakeStatusBarView != null) {            if (fakeStatusBarView.getVisibility() == View.GONE) {                fakeStatusBarView.setVisibility(View.VISIBLE);            }            fakeStatusBarView.setBackgroundColor(color);        } else {            decorView.addView(createStatusBarView(activity, color));        }        setRootView(activity);    }}

private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view;
/** * 生成一个和状态栏大小相同的彩色矩形条 * <p> * * @param activity * @param color * @param alpha * @return */private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) {    // 绘制一个和状态栏一样高的矩形    View statusBarView = new View(activity);    LinearLayout.LayoutParams params =            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));    statusBarView.setLayoutParams(params);    statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));    statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID);    return statusBarView;}
/** * 获取状态栏高度 * <p> * * @param context context * @return 状态栏高度 */private static int getStatusBarHeight(Context context) {    // 获得状态栏高度    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");    return context.getResources().getDimensionPixelSize(resourceId);}





package com.my.mystatus.activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.Toolbar;import android.view.View;import android.widget.Button;import com.my.mystatus.R;import com.my.mystatus.util.StatusBarUtil;import java.util.Random;public class SimpleBarColor extends AppCompatActivity implements View.OnClickListener {    private Button btnSimple, btnTitleBar, btnStatusTitleBar;    private Toolbar mToolbar;    private int mColor;    private Random random = new Random();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_simple_bar_color);        setBarColor();        btnSimple = (Button) findViewById(R.id.btn_simple);        btnSimple.setOnClickListener(this);        btnTitleBar = (Button) findViewById(R.id.btn_simple_title_bar_color);        btnTitleBar.setOnClickListener(this);        btnStatusTitleBar = (Button) findViewById(R.id.btn_simple_stauts_title_bar);        btnStatusTitleBar.setOnClickListener(this);        mToolbar = (Toolbar) findViewById(R.id.tb_simple);        mToolbar.setBackgroundColor(mColor);    }    private void setBarColor() {        // 设置默认的颜色        mColor = getResources().getColor(R.color.colorPrimary);        StatusBarUtil.setSimpleStatusBarColor(this, mColor);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_simple:                setStutusColor();                break;            case R.id.btn_simple_title_bar_color:                setTitleBarColor();                break;            case R.id.btn_simple_stauts_title_bar:                setStautsAndTitleBarColor();                break;        }    }    private void setStutusColor() {        simpleRandomColor();        StatusBarUtil.setSimpleStatusBarColor(this, mColor);    }    private void setTitleBarColor() {        simpleRandomColor();        mToolbar.setBackgroundColor(mColor);    }    private void setStautsAndTitleBarColor() {        simpleRandomColor();        StatusBarUtil.setSimpleStatusBarColor(this, mColor);        mToolbar.setBackgroundColor(mColor);    }    private void simpleRandomColor() {        mColor = 0xff000000 | random.nextInt(0xffffff);    }}

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#A1A4A4">    <android.support.v7.widget.Toolbar        android:id="@+id/tb_simple"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="标题栏" />    </android.support.v7.widget.Toolbar>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:orientation="vertical">        <Button            android:id="@+id/btn_simple"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:text="改变状态栏颜色" />        <Button            android:id="@+id/btn_simple_title_bar_color"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:layout_marginTop="20dp"            android:text="改变标题栏颜色" />        <Button            android:id="@+id/btn_simple_stauts_title_bar"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:layout_marginTop="20dp"            android:text="改变状态栏和标题栏颜色" />    </LinearLayout></RelativeLayout>


四、设置透明状态栏

效果图如下


相关代码:

/** * 设置全透明的状态栏 * * @param activity */public static void setTransparent(Activity activity) {    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {        return;    }    transparentStatusBar(activity);}

/** * 使状态栏透明 * * @param activity */private static void transparentStatusBar(Activity activity) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);        activity.getWindow().setStatusBarColor(Color.TRANSPARENT);    } else {        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);    }}

package com.my.mystatus.activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import com.my.mystatus.R;import com.my.mystatus.util.StatusBarUtil;public class TransparentActivity extends AppCompatActivity {    private Button btnChangImage;    private LinearLayout llBackground;    private int imageFlag = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_transparent);        setStatusBar();        llBackground = (LinearLayout) findViewById(R.id.ll_transparent);        btnChangImage = (Button) findViewById(R.id.btn_change_image);        btnChangImage.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (imageFlag == 0) {                    llBackground.setBackgroundResource(R.mipmap.hy2);                    imageFlag = 1;                } else {                    llBackground.setBackgroundResource(R.mipmap.hy1);                    imageFlag = 0;                }            }        });    }    private void setStatusBar() {        StatusBarUtil.setTransparent(this);    }}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/ll_transparent"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@mipmap/hy1"    android:gravity="center">    <Button        android:id="@+id/btn_change_image"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="20dp"        android:text="更换图片" /></LinearLayout>

参考文章:http://www.jcodecraeer.com/a/opensource/2016/0328/4095.html


原创粉丝点击