如何在 LayerList 的中心添加图像 android
how to add image at the center of LayerList android
我正在为启动画面设置样式 activity 并且在样式中我正在添加背景渐变,现在我正在尝试在屏幕中央添加图像,实际上就在屏幕中央下方,可以说是 20dp在中心下方。
谁能告诉我如何激活它。
我要添加到 activity 的样式:
<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 文件中。
您可以将图像存储在 drawawble 中并创建一个项目
<?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>
试试这个,对我有用
<?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>
我正在为启动画面设置样式 activity 并且在样式中我正在添加背景渐变,现在我正在尝试在屏幕中央添加图像,实际上就在屏幕中央下方,可以说是 20dp在中心下方。 谁能告诉我如何激活它。
我要添加到 activity 的样式:
<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 文件中。
您可以将图像存储在 drawawble 中并创建一个项目
<?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>
试试这个,对我有用
<?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>