简体   繁体   中英

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. Is it possible to add an image into gradient.xml?

Below is the xml code for this.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM