In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to click and change patterns and colors in Button in Android. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Basic framework basic properties
In the above xml file, Ctrl+ left-click into Button to see that this class inherits from the TextView class, which means that everything mentioned in the TextView section can be used on Button.
Text and background
Here we simply set the text on the button to "Push Me" and the background color to purple (both resources are saved in three xml files in the values folder), but strangely enough, the text is forced to uppercase, and setting the background is not valid.
Theme modification
The problem lies in the theme file themes.xml. We need to enter this file and set textAllCaps to false, so we can unforce uppercase, that is, add the following code:
True
Then we also need to change the basic theme file from
Change to
This will make our settings take effect:
Although the basic properties of the button are set, now this button looks no different from TextView, and there is no response to the click. Next, we need to set the click effect.
Click effect
The main effect to be achieved this time is to change the background of the button after clicking, so we create a new Drawable Resource file under the res- > drawable folder, which is named background_shift. As you can see, the root node of the file is the picture selector selector.
Picture transformation
Then we add some image resources, click res, right-click new- > Vector Asset, enter this interface, click Clip Art to select a favorite icon, for example, I selected WiFi:
At the same time, I also imported an icon with WiFi closed.
After import, it can be used in the background_shift file. Here, I use the state_pressed setting to display WiFi when pressed, and WiFi closed when not pressed. The code is as follows:
Once the background_shift.xml is written, it can be used in activity_main.xml, and the background transformation can be achieved by setting the background property of Button to this resource.
Color transformation
Color transformation is similar to image transformation, except that resources are stored in different locations. In the res folder, we create a new color folder, right-click rec, and create a new folder in new- > Directory. Name it color.
Then create a Drawable Resource file in color, and then set it to blue when pressed, and default to red:
With this color transformation file, we can set the backgroundTint of Button to this resource in activity_main.xml to achieve color transformation.
In addition to the background color, there is also the foregroud foreground color, which is the top color and covers the entire layer, which is not commonly used.
Thank you for reading! This is the end of the article on "how to click and change patterns and colors in Button in Android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.