美文网首页
Android Theme

Android Theme

作者: 小梦想家北冥有鱼 | 来源:发表于2017-05-31 23:19 被阅读0次

这里如果将ColorPrimary和colorPrimaryDark颜色值设置成一致,那么将会成为“沉浸式”,或者说是透明状态栏

 <style name="MyThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
     <!--选中状态icon的颜色和字体颜色-->
    <item name="colorPrimary">@color/colorTheme</item>
    <item name="colorPrimaryDark">@color/colorTheme</item>
    <item name="colorAccent">@color/colorTheme</item>
    <!--正常状态下字体颜色和icon颜色-->
    <!--<item name="android:textColorPrimary">@color/colorTheme</item>-->
</style>

注意:他们android不同版本上实现方法不同

image.png
 /**
 * 设置状态栏颜色
 *
 * @param activity       需要设置的activity
 * @param color          状态栏颜色值
 * @param statusBarAlpha 状态栏透明度
 */

public static void setColor(Activity activity, @ColorInt int color, int statusBarAlpha) {
    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().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      //版本是大于19,设置背景为透明
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        int count = decorView.getChildCount();
        if (count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView) {
            decorView.getChildAt(count - 1).setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
        } else {
            StatusBarView statusView = createStatusBarView(activity, color, statusBarAlpha);
            decorView.addView(statusView);
        }
        setRootView(activity);
    }
}


 /**
 * 计算状态栏颜色
 *
 * @param color color值
 * @param alpha alpha值
 * @return 最终的状态栏颜色
 */
private static int calculateStatusColor(@ColorInt int color, int alpha) {
    float a = 1 - alpha / 255f;
    int red = color >> 16 & 0xff;
    int green = color >> 8 & 0xff;
    int blue = color & 0xff;
    red = (int) (red * a + 0.5);
    green = (int) (green * a + 0.5);
    blue = (int) (blue * a + 0.5);
    return 0xff << 24 | red << 16 | green << 8 | blue;
}

相关文章

网友评论

      本文标题:Android Theme

      本文链接:https://www.haomeiwen.com/subject/gtndfxtx.html