简体   繁体   English

设置android app背景图片

[英]Setting android app background image

i want to set same background image to all my app layouts. 我想将相同的背景图像设置为我的所有应用布局。 My app supports all devices from mini phone to 10.1 tablets. 我的应用支持从迷你手机到10.1平板电脑的所有设备。

Is there one way to do this or i need to set for every layout re-sized image that quality would be good. 有没有一种方法可以做到这一点,或者我需要设置每个布局重新调整尺寸的图像,质量会很好。

I found this in one app: app_background.xml in drawable folder: 我在一个应用程序中找到了这个:drawable文件夹中的app_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffff00" />
</shape>   

Also maybe some one could give me good explanation how to separate design and functions in java. 也许有人可以给我很好的解释如何在java中分离设计和功能。

Thanks. 谢谢。

Use styles to do this. 使用样式执行此操作。
1. Create a appropriate drawable image for background 1.为背景​​创建适当的可绘制图像
2. Create a values/styles.xml file with following content 2.使用以下内容创建values / styles.xml文件

<style name="AppTheme" parent="android:style/Theme.Black">
      <item name="android:background">@drawable/main_app_bg</item>
   </style>


3. Use theme for whole app in AndroidManifest.xml 3.在AndroidManifest.xml中为整个应用程序使用主题

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme"> 

Well, you can configure a single image to be auto-scaled for each device's screen size, it explains how here: 那么,你可以配置一个图像来自动缩放每个设备的屏幕尺寸,它解释了如何:

http://developer.android.com/guide/practices/screens_support.html http://developer.android.com/guide/practices/screens_support.html

The issue with that is that if you want good aspect, you have to make your original image pretty big (as big as the biggest resolution you expect you app to be used on). 问题在于,如果你想要好的方面,你必须使你的原始图像非常大(与你期望应用程序使用的最大分辨率一样大)。 Then, when scaled down for smaller screens, that big picture will still use up more memory then needed. 然后,当按比例缩小以获得较小的屏幕时,该大图片仍会消耗更多内存。

In reality there're not so many different resolutions out there, maybe 7-8 major ones. 实际上,没有那么多不同的分辨率,可能有7-8个主要分辨率。 You should make individual background images of sizes specific to each target resolution, then, in your Action's onCreate(...) method you can do something like this to get the curren't device screen resolution: 您应该制作特定于每个目标分辨率的单个背景图像,然后,在Action的onCreate(...)方法中,您可以执行以下操作以获得当前设备屏幕分辨率:

Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();

and load the proper background for it (adjusting for eventual title and status bar sizes) 并加载适当的背景(调整最终的标题和状态栏大小)

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

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