SoFunction
Updated on 2025-03-08

Imitation of the Idios status bar color and title bar color are consistent

Create a tool class first

import ;
import ;
import ;
import ;
import .;
import ;
import ;

public class StatusBarCompat {
 private static final int INVALID_VAL = -1;
 //Judge version @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 public static View compat(Activity activity, int statusColor) {
  //Default color  int color = (activity, );
  //If the version is greater than or equal to 21, set the color to the status bar  if (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   if (statusColor != INVALID_VAL) {
    color = statusColor;
   }
   ().setStatusBarColor(color);
   return null;
  }
  //If the version is greater than or equal to 19 and less than 21  if (.SDK_INT >= Build.VERSION_CODES.KITKAT
    && .SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
   //Get contentView   ViewGroup contentView = (ViewGroup) ();
   if (statusColor != INVALID_VAL) {
    color = statusColor;
   }
   //Get the first view in the contentView, that is, the status bar   View statusBarView = (0);
   if (statusBarView != null && () == getStatusBarHeight(activity)) {
    (color);
    return statusBarView;
   }

   statusBarView = new View(activity);
    lp = new (.MATCH_PARENT,
     getStatusBarHeight(activity));
   (color);
   (statusBarView, lp);
   return statusBarView;
  }
  return null;
 }
 public static void compat(Activity activity) {
  compat(activity, INVALID_VAL);
 }
 public static int getStatusBarHeight(Context context) {
  int result = 0;
  int resourceId = ().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
   result = ().getDimensionPixelSize(resourceId);
  }
  return result;
 }
}

Then call the method in the activity

import .;
import ;
import .;
import ;
import ;
public class MainActivity extends AppCompatActivity {
 protected boolean statusBarCompat = true;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
  if (statusBarCompat) {
   (this, (this, ));
   transparent19and20();
  }
 }
 protected void transparent19and20() {
  if (.SDK_INT >= Build.VERSION_CODES.KITKAT
    && .SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
   getWindow().addFlags(.FLAG_TRANSLUCENT_STATUS);
  }
 }
}

There are a lot of copying from others on the Internet. The author should not criticize me, I am trying to find it and use it in the future.

The above example code with the same color as the title bar color of the IOS status bar is the entire content I share with you. I hope you can give you a reference and I hope you can support me more.