简体   繁体   English

如何在Android中将src属性中的图像添加到gradient.xml文件?

[英]How can I add image from src attribute to gradient.xml file in Android?

I am new to Android and I want to display image with gradient background. 我是Android的新手,我想显示带有渐变背景的图像。 Is it possible to add an image into gradient.xml? 是否可以将图像添加到gradient.xml?

Below is the xml code for this. 下面是用于此目的的xml代码。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <bitmap android:src="@drawable/bg"/>
    <shape>
        <gradient
            android:startColor="#631f68"
            android:endColor="#ff5555"
            android:angle="45"/>
    </shape>

</item>

Solution: 解:

In your main_activity.xml, 在您的main_activity.xml中,

set the background image for your topmost layout: (Relative or Constraint whatever you are using) 为最顶层的布局设置背景图片:(无论您使用的是相对还是约束)

Then take another layout and give height and width to match_parent and apply the below Gradient to the background of it. 然后采用另一种布局,并为match_parent提供高度和宽度,并将以下Gradient应用于其背景。

Gradient file: 渐变文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="135"
        android:centerColor="@color/colorAccentSecondary"
        android:endColor="@color/colorPrimary"
        android:startColor="@color/colorAccent"
        android:type="linear" />

    <corners android:radius="0dp" />
</shape>

Hope it helps. 希望能帮助到你。

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

相关问题 如何从cordova中的plugin.xml向android活动添加属性? - how can I add attribute to the android activity from plugin.xml in cordova? 如何从Java文件更改XML按钮属性? - How can I change XML button attribute from a java file? 如何通过xml为android中的按钮添加渐变? - How to add a gradient to buttons in android through xml? 如何在我的Android项目中将来自/ res / values的xml文件添加到gitignore中? - How can I add a xml file from /res/values to my gitignore in my Android project? 如何向工具栏添加颜色渐变(Android) - How can I add a color gradient to the toolbar (Android) 如何在Android中创建带有渐变的平行线图像? - How can I create an image of parallel lines with a gradient in Android? 如何在Android中向图像添加彩虹渐变 - How to add rainbow gradient to an Image in android 如何将数据从XML文件添加到ArrayList - How can I add data from an XML file to an ArrayList 如何在Android资源文件的Strings.xml中添加“布局”属性 - How to add “layout” attribute in Strings.xml of Android resource file 如何在android中添加一个主题属性到android:startColor in gradient drawable? - How to add a theme attribute to android:startColor in gradient drawable in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM