Android custom image and text function
Android custom image and text function
Write in four steps:
1. XML of combined control; 2. Customize the properties of the composite control; 3. Customize and inherit the class class of composite layout to realize the constructor with two parameters; 4. Display composite controls in XML.
Specific implementation process:
1、 XML for composite controls
There are two ways I contact. One is the XML of ordinary activity; One is XML whose parent node is merge. I use the first one in my project, but I personally feel that the second one is good, because the first one has more relative or absolute layout layers.
I wrote custom_ pictext.xml
Here is an example of merge. If you have time, you can experience it yourself.
Both XML are written in layout.
2、 Properties of custom composite controls
This step is an important part of our customization. The private features of custom components are displayed here.
First, create attrs. XML in values
Then define the attributes as follows
Here are a few points to note. First, the attribute name is name. Second, the attribute unit is from. The values contained in this unit can be viewed here.
3、 Customize the class class that inherits the composite layout and implements the constructor with two parameters
I implemented custompictext.java
Here, we can also add some listeners to the control, which we can add ourselves; What is worth noting here is a class typearray that loads the configuration
4. Display composite controls in XML
Just write this into an XML
The code is as follows
Don't forget to add xmlns: hman = "" http://schemas.Android.com/apk/res-auto ”
summary
The program is basically over.
Take a look at the screenshot of the effect
Thank you for reading, hope to help you, thank you for your support to this site!