Android Java 设置背景颜色为绿色
您可以使用以下代码来将 Android 应用程序的背景设置为绿色:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取根视图
View rootView = getWindow().getDecorView().getRootView();
// 设置背景颜色为绿色
rootView.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
}
}
请确保在 activity_main.xml 文件中设置了根视图的布局,例如:
<?xml version='1.0' encoding='utf-8'?>
<RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android'
xmlns:tools='http://schemas.android.com/tools'
android:id='@+id/activity_main'
android:layout_width='match_parent'
android:layout_height='match_parent'
android:paddingLeft='16dp'
android:paddingTop='16dp'
android:paddingRight='16dp'
android:paddingBottom='16dp'
tools:context='.MainActivity'>
<!-- 在此添加其他视图 -->
</RelativeLayout>
这将设置整个应用程序的背景颜色为绿色。您可以根据需要在 setBackgroundColor() 方法中更改颜色。
原文地址: http://www.cveoy.top/t/topic/bxOl 著作权归作者所有。请勿转载和采集!