3

我是 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);

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

4

1 回答 1

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

希望有帮助

于 2015-10-01T09:32:43.240 回答