我在android世界中相当新,而且我不确定如何正确修改Action栏。
在我的情况下,主要活动有一个FrameLayout
,我用导航抽屉在片段之间切换。
我已经看到有几种方法可以改变动作栏参数,但我不太确定哪一种是最好的。到目前为止我用来更改ActionBar(和片段)的内容如下:
@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 :(得分: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));
}
希望有所帮助