Android 如何将背景图片添加到活动中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3307090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:37:37  来源:igfitidea点击:

how to add background image to activity?

androidimagebackground

提问by Vladimir Berezkin

using theme or ImageView ?

使用主题或 ImageView ?

回答by Sephy

use the android:backgroundattribute in your xml. Easiest way if you want to apply it to a whole activity is to put it in the root of your layout. So if you have a RelativeLayout as the start of your xml, put it in here:

android:background在您的 xml 中使用该属性。如果要将其应用于整个活动,最简单的方法是将其放在布局的根目录中。所以如果你有一个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>

回答by Paresh Mayani

You can set the "background image" to an activity by setting android:backgroundxml attributes as followings:

您可以通过设置android:backgroundxml 属性为活动设置“背景图像”,如下所示:

(Here, for example, Take a LinearLayout for an activity and setting a background image for the layout(i.e. indirectly to an activity))

(这里,例如,为一个活动采取一个 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>

回答by Jesvin

Place the Image in the folder drawable. drawable folder is in res. drawable have 5 variants drawable-hdpi drawable-ldpi drawable-mdpi drawable-xhdpi drawable-xxhdpi

将图像放在 drawable 文件夹中。drawable 文件夹在 res. drawable 有 5 个变体 drawable-hdpi drawable-ldpi drawable-mdpi drawable-xhdpi drawable-xxhdpi

回答by Jorgesys

Nowadays we have to use match_parent:

现在我们必须使用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

在此处输入图片说明

回答by Sunil Kumar

We can easily place the background image in PercentFrameLayout using the ImageView. We have to set the scaleType attribute value="fitXY" and in the foreground we can also display other view's like textview or button.

我们可以使用 ImageView 轻松地将背景图像放置在 PercentFrameLayout 中。我们必须设置 scaleType 属性 value="fitXY" 并且在前台我们还可以显示其他视图,如文本视图或按钮。

 <android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        >
        <ImageView
            android:src="@drawable/logo"
            android:id="@+id/im1"
            android:scaleType="fitXY"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>
<EditText android:layout_gravity="center_horizontal"
        android:hint="Enter Username"
        android:id="@+id/et1"
        android:layout_height="wrap_content"
        app:layout_widthPercent="50%"
        app:layout_marginTopPercent="30%"
        />
<Button
    android:layout_gravity="center_horizontal"
    android:text="Login"
    android:id="@+id/b1"
    android:layout_height="wrap_content"
    app:layout_widthPercent="50%"
    app:layout_marginTopPercent="40%"/>
</android.support.percent.PercentFrameLayout>

回答by Sebastian Walla

and dont forget to clean your project after writing these lines you`ll a get an error in your xml file until you′ve cleaned your project in eclipse: Project->Clean...

并且不要忘记在编写这些行之后清理你的项目,你会在你的 xml 文件中得到一个错误,直到你在 eclipse 中清理了你的项目:Project->Clean...