溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android應(yīng)用中怎么實現(xiàn)自定義狀態(tài)欄

發(fā)布時間:2020-11-23 16:48:12 來源:億速云 閱讀:307 作者:Leah 欄目:移動開發(fā)

這篇文章給大家介紹Android應(yīng)用中怎么實現(xiàn)自定義狀態(tài)欄,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

一、目標(biāo):Android5.0以上

二、步驟

1、在res-values-colors.xml下新建一個RGB顏色

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

2、新建一個布局,名為actionbarlayout.xml,在后邊重寫布局時用于添加

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
      android:id="@+id/actionBarId" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 
  </LinearLayout> 

3、重寫LineaLayout布局,放置步驟二新建的布局在頂部,用作背景顏色的容器

  public class ActionBarLayOut extends LinearLayout { 
    public ActionBarLayOut(Context context, AttributeSet attrs){ 
      super(context,attrs); 
      LayoutInflater.from(context).inflate(R.layout.actionbarlayout,this); 
    } 
  }

3、在主布局里調(diào)用這個重寫后的線性布局

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
  <com.example.test.ActionBarLayOut xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.test.MainActivity"> 
    <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/theRed" 
      android:text="Hello World!" /> 
  </com.example.test.ActionBarLayOut> 

 4、在main活動中進行相應(yīng)設(shè)置

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      TextView textView = (TextView)findViewById(R.id.actionBarId); 
      int color = getResources().getColor(R.color.theRed); 
      setActionBarColor(textView,color); 
    } 
    protected void setActionBarColor(TextView textView, int ActionBarColor){ 
      //----------------------------------隱藏標(biāo)題欄---------------------------------------------- 
      if (getSupportActionBar()!=null) { 
        getSupportActionBar().hide(); 
      } 
      //------------------------------------------------------------------------------------------ 
      //----------------------------------將狀態(tài)欄設(shè)置為透明-------------------------------------- 
      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
        Window window = getWindow(); 
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS 
            | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); 
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
        window.setStatusBarColor(Color.TRANSPARENT); 
        window.setNavigationBarColor(Color.TRANSPARENT); 
      } 
      //------------------------------------------------------------------------------------------ 
      /** 
       * 首先獲取狀態(tài)欄的高度statusBarHeight1,然后在狀態(tài)欄的位置放一個空的TextView, 
       * 高度設(shè)置為statusBarHeight1,然后將TextView的背景顏色進行設(shè)置,這樣就可以變相 
       * 的給狀態(tài)欄設(shè)置顏色 
       */ 
      int statusBarHeight1 = -1; 
      //獲取status_bar_height資源的ID 
      int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 
      if (resourceId > 0) { 
        //根據(jù)資源ID獲取響應(yīng)的尺寸值 
        statusBarHeight1 = getResources().getDimensionPixelSize(resourceId); 
      } 
      textView.setHeight(statusBarHeight1); 
      textView.setBackgroundColor(ActionBarColor); 
    } 
  } 

關(guān)于Android應(yīng)用中怎么實現(xiàn)自定義狀態(tài)欄就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI