设置背景色和背景图片

本博客内容:
两种方式实现设置布局背景色

这里写图片描述

准备工作:
将准备的背景图片放在图片目录下,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);  //设置背颜色

猜你喜欢

转载自blog.csdn.net/qq_38340601/article/details/82284766