如何在Layer List android的中心添加图像

问题描述 投票:2回答:2

我正在为我的启动画面活动设置样式,并且在样式中我添加了背景渐变,现在我正在尝试在屏幕中心添加图像,实际上就在屏幕中心下方,可以说在中心下方20dp。任何人都可以告诉我如何激活它。

我正在添加到活动的样式:

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">
            @drawable/background_splash
        </item>
        <item name="android:buttonStyle">
            @style/Base.Widget.AppCompat.Button.Colored
        </item>
        <item name="colorAccent">
            @color/white
        </item>
        <item name="colorPrimaryDark">
            @color/blue.dark
        </item>
    </style> 

background_splash.xml:请参考文件中的注释

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/splash_screen_gradient" /> 
// here I want to add Image here such that it will be we wrap_contect and
// 20dp below center of screen
</layer-list>

splash_screen_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item >
        <shape>
            <gradient
                android:angle="270"
                android:centerColor="#ff0000"
                android:endColor="#00ff00"
                android:startColor="#0000ff">
            </gradient>
        </shape>
    </item>
</layer-list>

我通过在图层列表中添加位图,项目背景但是它没有按预期工作,尝试了几种排列组合。我不想在布局xml文件中添加它。

java android android-layout android-xml android-styles
2个回答
5
投票

试试这个,它对我有用

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

    <item android:drawable="@drawable/splash_screen_gradient" />
    <item android:top="20dp">
        <bitmap android:src="@drawable/ic_logo"
            android:gravity="center_vertical|center_horizontal" />
    </item>

</layer-list>

0
投票

您可以将图像存储在drawable中并创建项目

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/splash_screen_gradient" /> 
 <item android:drawable="@drawable/your_image" />
</layer-list>
© www.soinside.com 2019 - 2024. All rights reserved.