2016-04-05 228 views
0

我在操作欄中創建了一個字符串。我想要的是,當我點擊紅色按鈕時,背景會將其顏色更改爲紅色。有誰能告訴我我該如何實現這一目標?這裏是我的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> 
+0

屏幕的操作欄的背景? – Harry

+0

不,我說的是應用程序的背景 –

回答

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 

Happy_Coding;

+0

這將改變工具欄顏色不是應用程序的背景。 –

+0

檢查後'getWindow()。getDecorView().....'(更新)的最後一行 – Bharatesh

+0

爲該選定按鈕再寫一個if條件並用不同顏色代碼執行相同的行。 – Bharatesh

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; 
     } 
+0

我希望我編輯的代碼正確。你可以檢查 if(id == R.id.colourred)getSupportActionBar()。setBackgroundDrawable(new ColorDrawable(Color .parseColor(「FFFF00」))); 返回true; } –

+0

檢查此 getSupportActionBar()。setBackgroundDrawable(new ColorDrawable(Color .parseColor(「#FFFF00」))); –

+0

你想要應用程序總屏幕或操作欄的背景嗎? @Praveen –

0

此行只是添加到您的onOptionsItemSelected

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

你Main.java會是這個樣子:

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      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) { 
       getWindow().getDecorView().setBackgroundColor(Color.RED); 
      } 

      return super.onOptionsItemSelected(item); 
     } 
+0

我想更改應用程序背景而不是工具欄背景。 –

+0

通過你的問題的標題,我以爲你想改變工具欄的背景顏色。我編輯了我的答案。 –

+0

ohhh。所以很抱歉的混淆。 –

0

嘗試在你的代碼

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

插入由於這是否告訴ü,在菜單中選擇「colloured」項被選定, 所以在那裏你要處理它。

+0

謝謝。這工作正常。 –