如何将背景图像添加到活动?

时间:2010-07-22 08:39:29

标签: android image background

使用主题还是ImageView?

6 个答案:

答案 0 :(得分:113)

使用xml中的android:background属性。如果要将其应用于整个活动,最简单的方法是将其放在布局的根目录中。因此,如果您将RelativeLayout作为xml的开头,请将其放在此处:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootRL"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/background">
</RelativeLayout>

答案 1 :(得分:14)

您可以通过将android:background xml属性设置为以下内容,将“背景图片”设置为活动:

(例如,在这里,为活动选择一个LinearLayout并为布局设置背景图像(即间接地为活动))

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="@drawable/icon">
 </LinearLayout>

答案 2 :(得分:2)

将图像放在可绘制的文件夹中。可绘制的文件夹在res中。 drawable有5个变种 绘制,华电国际 绘制,LDPI 绘制,MDPI 绘制,xhdpi 抽拉-xxhdpi

答案 3 :(得分:1)

现在我们必须使用match_parent

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/background">
</RelativeLayout>

enter image description here

答案 4 :(得分:1)

我们可以使用ImageView轻松地将背景图像放在PercentFrameLayout中。我们必须设置scaleType属性value =“fitXY”,在前台我们还可以显示其他视图,如textview或button。

'/'

答案 5 :(得分:0)

并且不要忘记在写完这些行之后清理你的项目你会在你的xml文件中出现错误,直到你在eclipse中清理了你的项目:Project-&gt; Clean ...