在android棒棒糖或更高版本时设置windowTranslucentStatus = true

时间:2015-03-19 14:07:00

标签: java android api android-5.0-lollipop

当用户在棒棒糖及以上时,我想将windowTranslucentStatus设置为true,否则(至少在kitkat上)应用栏会出现在系统栏内。在棒棒糖上它很好。没有为每个版本制作单独的styles.xml,显然你不应该再做了,我怎样才能在java中设置它?

我的mainActivity中有以下代码但不知道hot windowTranslucentStatus ...有什么想法吗?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // set windowTranslucentStatus = true
}

2 个答案:

答案 0 :(得分:23)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window w = getWindow(); // in Activity's onCreate() for instance
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

答案 1 :(得分:4)

您可以随时在半透明或非透明之间切换:

public static void setTranslucent(Activity activity, boolean translucent){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  Window w = activity.getWindow();
  if(translucent) {
    w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  }
  else{
    w.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  }
}