3

我是Android的世界相當新的,我不知道如何正確修改操作欄。酒吧狀態不改變顏色和ActionBar的良好做法

在我的情況下,主要活動有一個FrameLayout,我用導航抽屜切換片段。

我已經看到,有幾種方法來更改操作欄參數,但我不太確定哪一個是最好的。什麼我使用至今改變動作條(和片段)如下:

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    public void displayView(int position) { 
     // update the main content by replacing fragment 
     Fragment fragment = null; 
     ImageView imageView = new ImageView(getSupportActionBar().getThemedContext()); 
     imageView.setScaleType(ImageView.ScaleType.CENTER); 
     switch (position) { 
      case 0: 
       imageView.setImageResource(R.drawable.ic_launcher); 
       getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.main_menu))); 
       fragment = new Seccion1MainMenu(); 
       break; 
      case 1: 
       imageView.setImageResource(R.drawable.ic_security); 
       getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.security_menu))); 
       fragment = new Seccion2Security(); 
       break; 
      case 2: 
       imageView.setImageResource(R.drawable.ic_light); 
       getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.light_menu))); 
       fragment = new Seccion3(); 
       break; 
      case 3: 
       imageView.setImageResource(R.drawable.ic_fan); 
       getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.fan_menu))); 
       fragment = new Seccion4(); 
       break; 

      default: 
       imageView.setImageResource(R.drawable.ic_launcher); 
       break; 
     } 
     if (fragment != null) { 
      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.frame_container, fragment).commit(); 

      // update selected item and title, then close the drawer 
      mDrawerList.setItemChecked(position, true); 
      mDrawerList.setSelection(position); 
      getSupportActionBar().setCustomView(imageView); 
      setTitle(navMenuTitles[position]); 
      mDrawerLayout.closeDrawer(mDrawerList); 
     } else { 
      // error in creating fragment 
      Log.e("Alvaro", "MainActivity - Error cuando se creo el fragment"); 
     } 

正如你可以在代碼中看到,我改變了操作欄,標題和圖像的顏色。這是改變內容的正確方式,還是我強制機器?

我無法做的是自動更改狀態欄的顏色。我已經閱讀了幾篇文章,但一直未能實現。建議的解決方案之一是添加此行:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 

但我沒有得到任何結果。你有什麼主意嗎?感謝您的幫助:)

+0

狀態欄您使用模擬器或真實設備幫助嗎? –

+0

我正在使用真實設備!我在其他職位閱讀與仿真器有時不工作 – Alvaro

+1

也許這可以幫助:http://stackoverflow.com/questions/29907615/android-transparent-status-bar-and-actionbar/29907616#29907616 – GuilhE

回答

2

狀態欄:

//changing statusbar 
if (android.os.Build.VERSION.SDK_INT >= 21){ 
       Window window = this.getWindow(); 
       window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
       window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
       window.setStatusBarColor(this.getResources().getColor(R.color.primary_dark)); 
      } 

這一項工作,希望它

+0

完美@Mochamad,感謝您的博文我做到了!如果API低於21,是否可以設置狀態欄顏色?謝謝? – Alvaro

+0

歡迎您 我覺得棒棒糖以下不支持對狀態欄顏色變化,但已經有人提出它在這個崗位工作 http://stackoverflow.com/questions/22192291/how-to-change-the-status-bar - 顏色 - 在 - 安卓 –