[英]Full screen background image in an activity
I see many applications that use a full-screen image as background.我看到许多使用全屏图像作为背景的应用程序。 This is an example:
这是一个例子:
I want to use this in a project, the best way I've found so far to do this is to use an image with a large size, put it in a ImageView
and use android: adjustViewBounds="true"
to adjust the margins我想在一个项目中使用它,到目前为止我发现最好的方法是使用大尺寸的图像,将它放在
ImageView
并使用android: adjustViewBounds="true"
来调整边距
The problem is that if a screen with a very high resolution, the image falls short.问题是,如果屏幕的分辨率非常高,则图像会不足。
Another option I thought of is to use the image in a FrameLayout
, with match_parent
in width
and height
as background... this stretches the image, but I think the result is not very good.我想到的另一个选择是在
FrameLayout
使用图像,以width
和height
match_parent
作为背景......这会拉伸图像,但我认为结果不是很好。
How would you do it?你会怎么做?
There are several ways you can do it.有几种方法可以做到。
Option 1:选项1:
Create different perfect images for different dpi and place them in related drawable folder.为不同的 dpi 创建不同的完美图像并将它们放在相关的可绘制文件夹中。 Then set
然后设置
android:background="@drawable/your_image"
Option 2:选项 2:
Add a single large image.添加单个大图像。 Use FrameLayout.
使用框架布局。 As a first child add an
ImageView
.作为第一个孩子添加
ImageView
。 Set the following in your ImageView.在您的 ImageView 中设置以下内容。
android:src="@drawable/your_image"
android:scaleType = "centerCrop"
Another option is to add a single image (not necessarily big) in the drawables (let's name it backgroung.jpg), create an ImageView iv_background at the root of your xml without a "src" attribute.另一种选择是在可绘制对象中添加单个图像(不一定很大)(让我们将其命名为 backgroung.jpg),在没有“src”属性的情况下在 xml 的根目录创建一个 ImageView iv_background。 Then in the onCreate method of the corresponding activity:
然后在对应activity的onCreate方法中:
/* create a full screen window */
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.your_activity);
/* adapt the image to the size of the display */
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(),R.drawable.background),size.x,size.y,true);
/* fill the background ImageView with the resized image */
ImageView iv_background = (ImageView) findViewById(R.id.iv_background);
iv_background.setImageBitmap(bmp);
No cropping, no many different sized images.没有裁剪,没有许多不同大小的图像。 Hope it helps!
希望能帮助到你!
You should put the various size images into the followings folder您应该将各种尺寸的图像放入以下文件夹中
for more detail visit this link有关更多详细信息,请访问此链接
ldpi低密度脂蛋白
mdpi MDPI
hdpi高清晰度电视
xhdpi xhdpi
xxhdpi xxhdpi
and use RelativeLayout or LinearLayout background instead of using ImageView as follwoing example并使用 RelativeLayout 或 LinearLayout 背景而不是使用 ImageView 作为以下示例
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/your_image">
</RelativeLayout>
It's been a while since this was posted, but this helped me.这个帖子发布已经有一段时间了,但这对我有帮助。
You can use nested layouts.您可以使用嵌套布局。 Start with a RelativeLayout, and place your ImageView in that.
从一个RelativeLayout 开始,然后把你的ImageView 放在里面。
Set height and width to match_parent to fill the screen.将高度和宽度设置为 match_parent 以填充屏幕。
Set scaleType="centreCrop" so the image fits the screen and doesn't stretch.设置 scaleType="centreCrop" 使图像适合屏幕并且不会拉伸。
Then you can put in any other layouts as you normally would, like the LinearLayout below.然后您可以像往常一样放入任何其他布局,例如下面的 LinearLayout。
You can use android:alpha to set the transparency of the image.您可以使用 android:alpha 来设置图像的透明度。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/image"
android:alpha="0.6"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There"/>
</LinearLayout>
</RelativeLayout>
What about关于什么
android:background="@drawable/your_image"
on the main layout of your activity?在您的活动的主要布局?
This way you can also have different images for different screen densities by placing them in the appropriate res/drawable-**dpi
folders.通过这种方式,您还可以通过将它们放置在适当的
res/drawable-**dpi
文件夹中来为不同的屏幕密度提供不同的图像。
use this用这个
android:background="@drawable/your_image"
in your activity very first linear or relative layout.在您的活动中,第一个线性或相对布局。
If you want your image to show BEHIND a transparent Action Bar, put the following into your Theme's style definition:如果您希望图像在透明的操作栏后面显示,请将以下内容放入主题的样式定义中:
<item name="android:windowActionBarOverlay">true</item>
Enjoy!享受!
与 NoToast 的答案一致,您需要在 res/drawable-ldpi、mdpi、hdpi、x-hdpi(对于超大屏幕)中有多个版本的“your_image”,删除 match_parent并保留android: adjustViewBounds="真的”
添加android:background="@drawable/your_image"
在您的 Relativelayout/Linearlayout Worked 中。
If you have bg.png as your background image then simply:如果您将 bg.png 作为背景图片,那么只需:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"/>
</RelativeLayout>
Working.在职的。 you should tryout this:
你应该试试这个:
android:src="@drawable/img"
three step for put background
1)you should choose your like picture. 1)你应该选择你喜欢的图片。 for example : enter image description here
例如:在此处输入图像描述
2)Then you copy this picture in drawable. 2)然后将这张图片复制到drawable中。 warning: you should choose types short for name picture.
警告:您应该选择名称图片的缩写类型。
enter image description here在此处输入图片说明
3)you go to page xml Intended and write : 3)你去页面xml意图并写:
android:background="id picture" for example my picture id is @drawable/download. android:background="id picture" 例如我的图片ID是@drawable/download。
enter image description here在此处输入图片说明
finish.结束。
The easiest way:最简单的方法:
Step 1: Open AndroidManifest.xml file第 1 步:打开 AndroidManifest.xml 文件
You can see the file here!你可以在这里看到文件!
Step 2: Locate android:theme="@style/AppTheme" >
第 2 步:找到
android:theme="@style/AppTheme" >
Step 3: Change to android:theme="@style/Theme.AppCompat.NoActionBar" >
第三步:改为
android:theme="@style/Theme.AppCompat.NoActionBar" >
Step 4: Then Add ImageView & Image第 4 步:然后添加 ImageView & Image
Step 4: That's it!第 4 步:就是这样!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.