如何以編程方式將活動的背景顏色設置爲白色?如何以編程方式將活動的背景顏色設置爲白色?
105
A
回答
119
得到一個處理使用的根佈局,然後設置背景色。根佈局就是你所謂的setContentView。
setContentView(R.layout.main);
// Now get a handle to any View contained
// within the main layout you are using
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView();
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
56
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:id="@+id/myScreen"
</LinearLayout>
換句話說,「android:background」是您想要更改的XML中的標記。
如果您需要動態更新的背景值,請參閱以下內容:
64
我喜歡着色由主題
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>
196
加入這一行中的活動,之後setContentView()
呼叫
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
7
你可以用它來打電話預定義的android顏色:
element.setBackgroundColor(android.R.color.red);
如果您想使用自己的自定義顏色之一,則可以將自定義顏色添加到strings.xml,然後使用下面的代碼調用它。
element.setBackgroundColor(R.color.mycolour);
但是,如果你想在你的layout.xml中設置顏色,你可以修改並添加下面的任何元素來接受它。所以
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
,改變顏色爲白色:
android:background="#FFFFFF"
3
要獲得在XML文件中定義的根認爲,如果不採取行動吧,你可以使用這個
root.setBackgroundResource(Color.WHITE);
3
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);
爲我工作。謝謝。
1
final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);
6
在你onCreate()
方法:
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));
你也需要添加到文件夾的值稱爲color.xml
一個新的XML文件,並分配有一個新的顏色屬性:
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_activity_background_color">#000000</color>
</resources>
請注意,您可以命名爲color.xml
任何你想要的名字,但你通過代碼引用它爲R.color.yourId
。
編輯
因爲getResources().getColor()
已過時,使用getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
代替。
1
Button btn;
View root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
root =findViewById(R.id.activity_main).getRootView();
root.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
});
}
相關問題
- 1. 如何以編程方式設置UILabel的背景顏色?
- 2. 如何以編程方式設置tablelayout行背景顏色
- 3. 如何將背景顏色設置爲白色
- 4. 設置活動的背景顏色
- 5. 以編程方式設置listview的背景顏色
- 6. 以編程方式設置形狀的背景顏色
- 7. 以編程方式將背景色調設置爲EditText視圖
- 8. Silverlight:以編程方式設置組合框背景顏色
- 9. NSProgressIndicator無法將背景顏色設置爲白色
- 10. 如何以編程方式爲數據網格行設置背景顏色
- 11. 如何以編程方式設置WatchKit應用程序的背景顏色?
- 12. Textfield背景色白色,活動灰色背景色
- 13. 如何爲GridViewColumn設置背景顏色
- 14. 如何以編程方式更改按鈕的背景顏色
- 15. 將背景顏色設置爲DotNet.Highcharts
- 16. 將背景顏色設置爲頁面
- 17. Qt:如何設置QPushButton的背景顏色爲系統顏色?
- 18. 設置Flash Builder 4.5移動應用程序TextArea背景顏色爲黑色,字體顏色爲白色
- 19. 將gridview背景顏色設置爲datatable的顏色?
- 20. 如何以編程方式獲取工具欄背景顏色?
- 21. 如何以編程方式更改對話框背景顏色?
- 22. CardView背景顏色始終爲白色
- 23. 如何設置tableView的背景顏色
- 24. 如何設置UIWebView的背景顏色
- 25. 如何設置UIWebView的背景顏色?
- 26. Angular4背景顏色設置
- 27. 設置VIM背景顏色
- 28. 設置背景顏色toolbaritems
- 29. 設置背景顏色CMDIFrameWnd
- 30. 設置行背景顏色
當我這樣做時,Eclipse將其標記爲「應該在這裏通過解析顏色而不是資源ID:getResources()。getColor(android.R.color.red)」。 – joriki 2013-08-06 13:03:25