Android 全屏展示

在Android 4.4以后,引入了Translucent System Bar的系特性,用于解决系统通知栏问题。


Android 全屏展示_第1张图片
image.png

Android 全屏展示_第2张图片
image.png

系统的通知栏和app界面融为一体,不再是孤立通知栏了。
但是这两个是有点区别的下面细说。

具体操作 方法一

(系统状态栏一般25dp)

  • 主要修改的操作在 style.xml 文件中。

  • 要在Activity中使用 Translucent System Bar 特性,首先需要到AndroidManifest中为指定的Activity设置Theme, 注意 不能直接在values/style.xml直接去自定义 Translucet System Bar 的Theme,因为特性具有SDK版本 的限制,所以要分别创建values、values-v19/style.xml、values-v21/style.xml文件然后就可以操作了。在对应SDK版本中定制对应的样式了。

  • 在Android 4.4之前的版本上运行,直接跟随系统主题



  • 在Android 4.4 版本上运行 values-v19/style.xml

  • 在Android 5.0 版本及以上上运行 values-v21/style.xml

注意,这三个文件中的values/style.mxl对应的主题theme名称要相同。

第一个图片样式

在AndroidManifest.xml中对指定Activity的theme样式
并在此activity的根布局中加入:
android:fitsSystemWindows="true"
把背景替换为要演示的图片即可

第二种图片样式

Tab栏和系统导航栏分开来设置。


方法二 代码方式实现

通过代码获取系统状态栏并对其修改

public class TestActivity extends AppCompatActivity {
  //在Activity的setContentView后进行修改系统状态栏
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        setStatusBar();
    }
    // 为状态栏添加默认样式
    protected void setStatusBar(int color, int statusBarAlpha) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // 版本号大于5.0
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 版本号大于4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            ViewGroup decorView = (ViewGroup) 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(calculateStatusColor(color, statusBarAlpha));
            } else {
                decorView.addView(createStatusBarView(this, color, statusBarAlpha));
            }
            setRootView(this);
        } 
    }

    /**
     * 生成一个和状态栏大小相同的半透明矩形条
     * @param activity 需要设置的activity
     * @param color    状态栏颜色值
     * @param alpha    透明值
     * @return 状态栏矩形条
     */
    private static View createStatusBarView(@ColorInt int color, int alpha) {
        // 获得状态栏高度
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        // 绘制一个和状态栏一样高的矩形
        View statusBarView = new View(this);
        LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(resourceId));
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
        statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID);
        return statusBarView;
    }

    // 设置根布局参数
    private static void setRootView() {
        ViewGroup parent = (ViewGroup) findViewById(android.R.id.content);
        for (int i = 0, count = parent.getChildCount(); i < count; i++) {
            View childView = parent.getChildAt(i);
            if (childView instanceof ViewGroup) {
                childView.setFitsSystemWindows(true);
                ((ViewGroup) childView).setClipToPadding(true);
            }
        }
    }

    /**
     * 计算状态栏颜色
     * @param color color值
     * @param alpha alpha值
     * @return 最终的状态栏颜色
     */
    private static int calculateStatusColor(@ColorInt int color, int alpha) {
        if (alpha == 0) {
            return color;
        }
        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;
    }
}

首先在一个BaseActivity中做些工作

你可能感兴趣的:(Android 全屏展示)