全屏背景上的android xml drawable图像 [英] android xml drawable image on full screen background

查看:60
本文介绍了全屏背景上的android xml drawable图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android中需要一个xml drawable(用于cordova启动画面).我想在屏幕上居中显示一个透明徽标(无拉伸),而屏幕的其余部分设置了背景颜色.

I need an xml drawable (for a cordova splash screen) in android. I Want to display a transparent logo centered on the screen (without stretching), while the rest of the screen has a background color set.

我首先尝试的是仅在xml文件中添加图像:

What I tried first, was to add only the image in the xml file:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    android:src="@drawable/splashscreen"
    android:gravity="center_horizontal|center_vertical" />

这显示徽标在屏幕上居中,但(显然)没有背景色.

This showed the logo centered on the screen, but (obviously) has no background color.

因此,我尝试了不同的解决方案来为该xml添加背景色.例如:

So I tried different solutions to add a background color to that xml. For example:

<?xml version="1.0" encoding="utf-8"?>    
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
    <item>
        <bitmap
            android:src="@drawable/splashscreen"
            android:gravity="center_horizontal|center_vertical" />
    </item>
</layer-list>

现在图像具有背景色-但它不再是全屏显示.全屏显示时,它会根据图像大小进行调整并拉伸.

Now the image has a background color - but it is not full screen any more. It adjusts to the image size and gets stretched when showing as full screen.

那么,如何获得具有背景色的全屏可绘制图像以及居中的图像呢?

So, how can I get a full screen drawable with background color, and a centered image on it?

所有答案都建议使用RelativeLayout-但我认为这在"drawable"的xml文件中是不可能的,对吧?我没有可以编辑的任何布局,只有可绘制对象引用了可绘制对象.

All answers suggest using RelativeLayout - but I think this is not possible within an xml file in "drawable", right? I do not have any layout i can edit - only a drawable which gets referenced by cordova.

推荐答案

我自己找到了解决方案:

I found the solution myself:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="#FFFF"></color>
    </item>
    <item>
        <bitmap
            android:src="@drawable/splashscreen"
            android:gravity="center_horizontal|center_vertical"
            />
    </item>
</layer-list>

Android Studio显示布局仍然错误-但可以按预期在设备上运行.

Android studio shows the layout still wrong - but it works on the device as expected.

对于所有张贴者,建议添加一个layout-xml文件:正如我在问题中所述,我没有这样做的可能性,因为此可绘制对象来自Cordova.我无法控制此drawable周围的已用布局.

To all posters, suggesting adding a layout-xml file: As I stated in the question, I don't have the possibility to do that, since this drawable gets included from Cordova. I don't have control over the used layout around this drawable.

这篇关于全屏背景上的android xml drawable图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆