【问题标题】:Bar status not changing color and ActionBar good practices栏状态不改变颜色和 ActionBar 良好做法
【发布时间】:2015-09-30 00:02:16
【问题描述】:

我是 android 世界的新手,我不确定如何正确修改操作栏。

就我而言,主要活动有一个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);

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

【问题讨论】:

  • 状态栏你用的是模拟器还是真机?
  • 我用的是真机!我在其他帖子中读到,使用模拟器有时它不起作用
  • 也许这会有所帮助:stackoverflow.com/questions/29907615/…
  • 谢谢@GuilhE 你的帖子真的很有帮助! :)
  • @Alvaro 很高兴我能帮上忙!如果您认为值得,请为我的帖子投票 :)

标签: android android-fragments android-actionbar android-statusbar


【解决方案1】:

状态栏:

这个对我有用

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

希望对你有帮助

【讨论】:

  • 完美的@Mochamad,感谢您的帖子,我成功了!如果 API 低于 21,是否可以设置状态栏颜色?谢谢?
  • 不客气,我认为下面的棒棒糖不支持状态栏颜色更改,但有人已经在这篇文章中使用它stackoverflow.com/questions/22192291/…
猜你喜欢
  • 1970-01-01
  • 2014-12-17
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多