Câu trả lời:
Nhận một điều khiển để bố trí gốc được sử dụng, sau đó đặt màu nền trên đó. Bố cục gốc là bất cứ thứ gì bạn gọi là 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));
root.setBackgroundColor(getResources().getColor(android.R.color.red));
Thêm dòng duy nhất này vào hoạt động của bạn, sau setContentView()
cuộc gọi
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
Tôi thích tô màu theo chủ đề
<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>
android:windowBackground
hiển thị trước, trong một khoảnh khắc ngắn, và sau đó màu nền bố cục sẽ chiếm. Vì vậy, nếu bạn sử dụng hai màu khác nhau, nó sẽ nhấp nháy trên màn hình.
windowBackground
chỉ ảnh hưởng đến nền cửa sổ, nhưng cũng colorBackground
ảnh hưởng đến tất cả các chế độ xem. stackoverflow.com/questions/26266221/ từ
?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>
Nói cách khác, "android: nền" là thẻ trong XML mà bạn muốn thay đổi.
Nếu bạn cần cập nhật động giá trị nền, hãy xem phần sau:
Trong onCreate()
phương pháp của bạn :
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));
Ngoài ra, bạn cần thêm vào thư mục giá trị một tệp XML mới được gọi color.xml
và Gán ở đó một thuộc tính màu mới:
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_activity_background_color">#000000</color>
</resources>
Lưu ý rằng bạn có thể đặt tên cho color.xml
bất kỳ tên nào bạn muốn nhưng bạn gọi nó bằng mã như R.color.yourId
.
BIÊN TẬP
Bởi vì getResources().getColor()
bị phản đối, sử dụng getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
thay thế.
Bạn có thể sử dụng điều này để gọi các màu Android được xác định trước:
element.setBackgroundColor(android.R.color.red);
Nếu bạn muốn sử dụng một trong các màu tùy chỉnh của riêng mình, bạn có thể thêm màu tùy chỉnh của mình vào chuỗi String và sau đó sử dụng màu bên dưới để gọi nó.
element.setBackgroundColor(R.color.mycolour);
Tuy nhiên, nếu bạn muốn đặt màu trong layout.xml của mình, bạn có thể sửa đổi và thêm phần bên dưới vào bất kỳ phần tử nào chấp nhận nó.
android:background="#FFFFFF"
Để có được chế độ xem gốc được xác định trong tệp xml của bạn, không có thanh hành động, bạn có thể sử dụng:
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
Vì vậy, để thay đổi màu thành màu trắng:
root.setBackgroundResource(Color.WHITE);
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);
đã làm cho tôi. cảm ơn bạn.
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"));
}
});
}