本博客内容:
两种方式实现设置布局背景色
准备工作:
将准备的背景图片放在图片目录下,drawable和mipmap测试后,都可以,暂时未发现有什么不同。
第一部分:
第一种方法添加背景图片:
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/clound"
android:id="@+id/linearLayout_mainAty"
tools:context=".MainActivity">
</LinearLayout>
第二种方式添加背景图片
MainActivity.java
setContentView(R.layout.activity_main);
getWindow().setBackgroundDrawableResource(R.drawable.clound);
第二部分
添加背景色:
activity.xml
使用android:background="#00ff00"
MainActivity.java
//设置背景色
View viewById = findViewById(R.id.linearLayout_mainAty); //获取布局id
viewById.setBackgroundColor(Color.BLUE); //设置背颜色