更改屏幕的背景颜色

时间:2016-04-05 13:27:08

标签: java android

我在操作栏中创建了一个字符串。我想要的是,当我点击红色按钮时,背景会将其颜色更改为红色。谁能告诉我如何实现这一目标?这是我的Main.java文件:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        setFrameVisibility(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.colourred) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

这是xml文件

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/colourred"
    android:orderInCategory="100"
    android:title="@string/colour_red"
    app:showAsAction="never" />
</menu>

4 个答案:

答案 0 :(得分:0)

在onOptionsItemSelected中,如果是id == R.id.colourred,则通过设置操作栏来更改颜色

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color
                    .parseColor("red color code")));

onOptionsItemSelected的应用程序屏幕背景

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.colourred) {

         findViewById(R.id.root layout id).setBackgroundColor(Color.RED);

            return true;
        }

答案 1 :(得分:0)

只需将此行添加到class B : public A { public: B() : B(0,0) {}; B(int a) : B(a,0) {}; B(double d) : B(0,d) {}; B(double a, int d) {/*full implementation*/}; };

即可
onOptionsItemSelected

你的Main.java看起来像这样:

getWindow().getDecorView().setBackgroundColor(Color.RED);

答案 2 :(得分:0)

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.colourred) {
        //toolBar.setBackgroundColor(Color.RED);
        getWindow().getDecorView().setBackgroundColor(Color.RED);
        getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FFCC33"));
        //or like below with color code
        //toolBar.setBackgroundColor(Color.parseColor("#FFCC33"));
        return true;
    }else if(id==R.id.color_green){
        getWindow().getDecorView().setBackgroundColor(Color.parseColor("#green_color_code"));
    }else if(id==R.id.other_id){
        getWindow().getDecorView().setBackgroundColor(Color.parseColor("#other_color_code"));
    }
}

如果您想更改活动背景颜色,请访问此How to set background color of Activity to white programmatically?

getWindow().getDecorView().setBackgroundColor(Color.WHITE);//change activity bg color

答案 3 :(得分:0)

尝试插入代码

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    ...
    //noinspection SimplifiableIfStatement
    if (id == R.id.colourred) {
        getWindow().getDecorView().setBackgroundColor(Color.RED);
        return true;
    }
    ...
}

因为这可以告诉你你的'#coll;&#39;菜单中的项目被选中, 所以在那里你想要处理它。