Setting selector for ViewGroup in Android does not work

Source: Internet
Author: User


When I was working on a project today, the leader wanted an interface similar to the setting in QQ, that is, the display effect of background using the rounded border. As follows:



After each entry is clicked, different colors are displayed, as shown below: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140412/2014041209050684.png" alt = "\">


This effect is actually easy to implement. The rounded border uses the shape image as the background. Click entries to display effects of different colors using the background selector. Both shape and selector are image elements in android. I used the shape image to set the rounded corner and used it as the background to design the following simple interface:


The upper and lower parts and the middle split lines are placed in a vertical linear layout, which uses a rounded shape as the background. Both the top and bottom entries are RelativeLayout.


The next step is to set the background selector for each entry so that blue is displayed as the background when the entry is clicked. The background selector round_corner_top_item_bg.xml written for the first entry is as follows:

     
          
               
                
                
            
               
          
          
               
                
                
            
       
  
 

Set the background for the first entry:

android:background="@drawable/round_corner_top_item_bg"

I thought it was super simple, but this selector did not work in any way when testing the program !!! The selector file has been checked many times and no error is found. This makes me crazy. Then I thought about whether it was because the selector was set for ViewGroup, rather than the selector set for a single control. Therefore, I added a Button to the layout and set the selector to the Button, which is effective. Can ViewGroup not use selector? I searched the internet for half a day and did not find the answer.


So I decided to write the code Logic first and set the click listener for this entry in the Code:

titleLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.i("xxx", "click title");}}) ;

At this time, something strange happened! Selector has an effect!

The event transfer mechanism of Android components is related. Readers who know the reason also hope to give us some advice.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.