SoFunction
Updated on 2025-03-11

Android programming method to change the background and form of controls

This article describes the method of Android programming to change the background and form of the control. Share it for your reference, as follows:

1. Change the background

Create an XML file under res/drawable:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:andro>
    // Background when the control is pressed  <item android:state_pressed="true" android:drawable="@drawable/search_bar_edit_pressed" />
      // Background when the control is selected  <item android:state_selected="true" android:drawable="@drawable/search_bar_edit_pressed"/>
    // Background in general state of the control  <item android:drawable="@drawable/search_bar_edit_normal" />
</selector>

2. Change the form

<shape>
    // Gradient  <gradient android:startColor="#8600ff" />
    // Stroke  <stroke android:width="2dp" android:color="#000000" />
    // Round corners  <corners android:radius="5dp" />
    // Margin  <padding android:left="10dp" android:top="10dp"
    android:bottom="10dp" android:right="10dp" />
</shape> 

Reference on the layout file:

android:background="@drawable/"

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android layout layout tips summary》、《Android database operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.