SoFunction
Updated on 2025-04-04

Android layout method of LinearLayout to customize highlighted background

This article describes the method of LinearLayout custom highlighting background in Android layout. Share it for your reference, as follows:

First create the linearlayout_background.xml file

res/drawable/linearlayout_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selectorxmlns:andro>
  <itemandroid:state_pressed="true" android:drawable="@drawable/button_pressed"/>
  <itemandroid:state_focused="true" android:drawable="@drawable/button_pressed"/> 
   <itemandroid:drawable="@drawable/button_normal"/>
</selector>

Secondly, in the corresponding xml layout file

&lt;LinearLayout
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:clickable="true"&lt;!--This is a must,Otherwise, there is no highlighting effect,This is different from automaticbutton,textviewOther controls--&gt;
    android:background="@drawable/linearlayout_background"&gt; 
&lt;Button ....../&gt;
&lt;TextView ...../&gt;
&lt;/LinearLayout&gt;

For more information about Android layout, readers who are interested in view the topic of this site:Android layout layout tips summary

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