简体   繁体   English

使用可绘制图像作为背景

[英]Use Drawable image as Background

如何在线性布局中设置背景图像,使其适合所有设备,该图像的分辨率是多少?

If you want to set through xml, then you need to do as below: 如果要通过xml进行设置,则需要执行以下操作:

android:background="@android:color/white"

in case if you decide to use android's default color code or if you have colors specified in colors.xml, then use 如果您决定使用android的默认颜色代码或在colors.xml中指定了颜色,则使用

android:background="@colors/white"

If you want to do programmatically, then do: 如果要以编程方式执行操作,请执行以下操作:

linearlayout.setBackgroundColor(Color.WHITE);

and if you want to set an image 如果您想设置图像

android:background="@drawable/image"

Take this repeatable drawable app_background.xml: 使用以下可重复绘制的app_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/your_image"
        android:tileMode="repeat" />

And use it like this: 并像这样使用它:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_background"/>

And if you want to scale your_image to all screen resolutions, define it as a 9-patch image: http://developer.android.com/tools/help/draw9patch.html 而且,如果您想将your_image缩放到所有屏幕分辨率,请将其定义为9补丁图像: http : //developer.android.com/tools/help/draw9patch.html

Use this link for supporting different screen sizes. 使用此链接可支持不同的屏幕尺寸。 You will have to make same image with different resolutions 您将不得不使用不同的分辨率制作相同的图像

And to set the image as background, 并将图像设置为背景,

usage: 用法:

android:background:"@drawable/background"

You can keep your image in res/drawable folders or create a folder inside res main folder which is name drawable-nodpi which is support all dpi device. 您可以将图像保存在res / drawable文件夹中,也可以在res主文件夹中创建一个名为drawable-nodpi的文件夹,该文件夹支持所有dpi设备。 and also you can follow Simple Nine-patch Generator 而且您还可以关注简单的九补丁生成器

then set background image thus. 然后设置背景图像。 such as

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher">

Best of luck! 祝你好运!

For using a background from drawable folder, 要使用可绘制文件夹中的背景,

android:background="@drawable/background" >

prepare 4 images and place them in their respective folders. 准备4张图像,并将它们放在各自的文件夹中。

xhdpi (xlarge screens) 960dp x 720dp xhdpi (超大屏幕)960dp x 720dp

hdpi (large screens) 640dp x 480dp hdpi (大萤幕)640dp x 480dp

mdpi (normal screens) 470dp x 320dp mdpi (正常屏幕)470dp x 320dp

ldpi (small screens) 426dp x 320dp ldpi (小萤幕)426dp x 320dp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM