在android编程中插入背景图片

<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="@drawable/qq"
    >
上面的代码是把drawable下的名字为qq的图片作为背景

我知道的有两种方法。首先你先将一个.png的背景图片存入工程中res/drawble(当然drawble-hdpi、drawble-mdpi、drawble-ldpi中一个或者几个文件夹都可)文件夹中。假如我存入的图片名称为activity_bg.png

第一种,在对应的布局文件(自动生成的工程默认文件是在res/layout中的main.xml)中的LinearLayout(默认)属性中添加android:background="@drawable/activity_bg"

第二种,在你要添加背景的activity的类文件中setContentView(R.layout.main);(默认)之后添加一句getWindow().setBackgroundDrawableResource(R.drawable.activity_bg);

两种方法都能实现在activity中添加背景,第二种方法一定要先setContentView()再设背景,不然报错。

原文地址:https://www.cnblogs.com/LiaoHao/p/3290963.html