Set background image on tab in Android

I want to set the background (red image) on the selected tab bar.

I originally installed as

enter image description here

When i change the code

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected, use grey --> <item android:drawable="@drawable/selected_tab" android:state_selected="true" /> </selector> 

It looks like

enter image description here

My requirement is to look below. Please help achieve this.

enter image description here

+4
source share
2 answers

you need to do this:

For the background of the tab, you need to create a selector this way

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected, use grey --> <item android:drawable="@drawable/selected_tab" android:state_selected="true" /> <item android:drawable="@drawable/default_tab"/> </selector> 

Now you can create a button with a different layout for your tab button, like this path. For this Button View requirement, set the tab icon on the top with the text.

tab_add_photo_btn.xml

 <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/tab_search" android:background="@drawable/tab_selector" android:drawableTop="@drawable/add_photo_icon" android:text="ADD PHOTO" android:textColor="#fff" android:padding="10dp"/> 
+1
source

I feel this has nothing to do with XML coding .. just do some design trick

you need to make two different twiky images for each bookmark icon.

  • make the background image of the background, which has a small upper part transparent (not selected resource)

  • make the selected image with the same size containing the selected background (selected resource)

0
source

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


All Articles