SoFunction
Updated on 2025-04-09

Android custom status bar instance code

1. Target: Android 5.0 or above

2. Steps

1. Create a new RGB color below

  <?xml version="1.0" encoding="utf-8"?> 
  <resources> 
    <color name="colorPrimary">#3F51B5</color> 
    <color name="colorPrimaryDark">#303F9F</color> 
    <color name="colorAccent">#FF4081</color> 
    <color name="theRed">#ff6a69</color> 
  </resources> 

2. Create a new layout, named, used to add when rewriting the layout later

<?xml version="1.0" encoding="utf-8"?> 
  <LinearLayout xmlns:andro 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
      android: 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 
  </LinearLayout> 

3. Rewrite the LineaLayout layout, place the newly created layout in step 2 on the top, and use it as a container for background color

  public class ActionBarLayOut extends LinearLayout { 
    public ActionBarLayOut(Context context, AttributeSet attrs){ 
      super(context,attrs); 
      (context).inflate(,this); 
    } 
  }

3. Call this rewrited linear layout in the main layout

<?xml version="1.0" encoding="utf-8"?> 
  < xmlns:andro 
    xmlns:tools="/tools" 
    android: 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=""> 
    <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/theRed" 
      android:text="Hello World!" /> 
  </> 

 4. Make corresponding settings in main activities

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      (savedInstanceState); 
      setContentView(.activity_main); 
      TextView textView = (TextView)findViewById(); 
      int color = getResources().getColor(); 
      setActionBarColor(textView,color); 
    } 
    protected void setActionBarColor(TextView textView, int ActionBarColor){ 
      //-----------------------------------------------------------------------------------------------------------------------------      if (getSupportActionBar()!=null) { 
        getSupportActionBar().hide(); 
      } 
      //------------------------------------------------------------------------------------------ 
      //-----------------------------------------------------------------------------------------------------------------------------      if(.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) { 
        Window window = getWindow(); 
        (.FLAG_TRANSLUCENT_STATUS 
            | .FLAG_TRANSLUCENT_NAVIGATION); 
        ().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); 
        (.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
        (); 
        (); 
      } 
      //------------------------------------------------------------------------------------------ 
      /**
        * First get the status bar height statusBarHeight1, and then put an empty TextView in the status bar position.
        * Set the height to statusBarHeight1, and then set the background color of the TextView, so that you can distort it
        * Set the color of the status bar
        */ 
      int statusBarHeight1 = -1; 
      //Get the ID of the status_bar_height resource      int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 
      if (resourceId &gt; 0) { 
        //Get the size value of the response based on the resource ID        statusBarHeight1 = getResources().getDimensionPixelSize(resourceId); 
      } 
      (statusBarHeight1); 
      (ActionBarColor); 
    } 
  } 

The above is the Android custom status bar example code introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!