Set Background Image and XML Resource

I use the code below to round the corners of a RelativeLayout. I save this as mybackground.xml in the drawable folder.
It works great for rounding a corner, but the problem is that I also want to add a transparent image as the background of my RelativeLayout. How can I achieve both things? How can I use image and drawn xml (to round a corner) at the same time for RelativeLayout ...

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <corners android:bottomRightRadius="30dp" android:bottomLeftRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="30dp" /> </shape> 
+6
source share
4 answers

You can use Layer-List

It will be like

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle" android:padding="10dp"> <corners android:bottomRightRadius="30dp" android:bottomLeftRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="30dp" /> </shape> </item> <item><!-- your transparent image --></item> </layer-list> 
+6
source

use the Layer List and Item tag to set the Image and use the solid tag and set the color to # AA000000 for transparent as shown below

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <solid android:color="#AA000000"/> <corners android:bottomRightRadius="30dp" android:bottomLeftRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="30dp" /> </shape> </item> <item> <bitmap android:src="@drawable/yourfilename"/> </item> </layer-list> 
+17
source

Try something like this:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <corners android:bottomRightRadius="30dp" android:bottomLeftRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="30dp" /> </shape> </item> <item> <bitmap android:src="@drawable/background"/> </item> </layer-list> 
+1
source

It works. If you do not surround the corners of your drawing, you too cannot see the rounded corner. Some friends talked about this in previous answers. They must round their image to see the effect of rounding.

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/listviewback2"> <shape android:shape="rectangle" android:padding="10dp"> <corners android:bottomRightRadius="35dp" android:bottomLeftRadius="35dp" android:topLeftRadius="35dp" android:topRightRadius="35dp"/> </shape> </item> </layer-list> 
+1
source

Source: https://habr.com/ru/post/892227/


All Articles