在全屏幕背景的机器人xml drawable图象

时间:2015-04-23 08:23:50

标签: android xml drawable

我需要在android中使用xml drawable(用于cordova启动画面)。我希望在屏幕上显示一个透明徽标(不拉伸),而屏幕的其余部分则设置背景颜色。

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

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

这表明徽标以屏幕为中心,但(显然)没有背景颜色。

所以我尝试了不同的解决方案来为该xml添加背景颜色。例如:

<?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>

现在图像有背景颜色 - 但它不再是全屏。它会调整为图像大小,并在显示为全屏时拉伸。

那么,我怎样才能获得一个全屏可绘制的背景颜色和一个居中的图像呢?

修改 所有答案建议使用RelativeLayout - 但我认为这在&#34; drawable&#34;中的xml文件中是不可能的,对吧?我没有任何可以编辑的布局 - 只有一个可由cordova引用的drawable。

5 个答案:

答案 0 :(得分:18)

我自己找到了解决方案:

<?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工作室显示布局仍然错误 - 但它可以按预期在设备上运行。

对于所有海报,建议添加一个layout-xml文件:正如我在问题中所述,我没有可能这样做,因为这个drawable包含在Cordova中。我无法控制这个drawable周围的使用布局。

答案 1 :(得分:6)

<?xml version="1.0" encoding="UTF-8"?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
        android:src="@drawable/splash" 
        android:gravity="fill_horizontal|fill_vertical" />

答案 2 :(得分:1)

这样做,

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white" >//Set background color

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:src="@drawable/splash_logo" />//set logo 

    </RelativeLayout>

答案 3 :(得分:0)

您可以根据需要使用以下代码:

   <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@color/bg_color"/>
    <item>
        <bitmap
            android:gravity="center|bottom|clip_vertical"
            android:src="@drawable/your_image" />
    </item>

</layer-list>

答案 4 :(得分:-1)

无需为背景颜色使用其他相对布局。 您可以使用单个ImageView执行此操作,如:

;
相关问题