Bar状态不改变颜色和ActionBar的良好做法

Bar status not changing color and ActionBar good practices

我对 android 的世界还很陌生,我不确定如何正确修改操作栏。

在我的例子中,主要 activity 有一个 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);

但是我没有得到结果。你有什么主意吗?感谢您的帮助:)

对于状态栏:

这个对我有用

//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));
            }

希望对您有所帮助