我是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);
但我沒有得到任何結果。你有什麼主意嗎?感謝您的幫助:)
狀態欄您使用模擬器或真實設備幫助嗎? –
我正在使用真實設備!我在其他職位閱讀與仿真器有時不工作 – Alvaro
也許這可以幫助:http://stackoverflow.com/questions/29907615/android-transparent-status-bar-and-actionbar/29907616#29907616 – GuilhE