Android implements the method of customizing buttons with text and pictures

This article describes the method of customizing buttons with text and pictures for Android. Share with you for your reference. The specific analysis is as follows:

Buttons with text and pictures are often used in Android development. Let's explain the common implementation methods.

1、 It is implemented with the button provided by the system

The simplest way is to use the button provided by the system, which has the smallest amount of code. One of the attributes of the button is drawableleft. This attribute can set the picture to the left of the text, but this method must make the background color of the icon transparent. If the background color of the icon is not transparent, the background color of the icon will not change when the button is clicked.

Main code:

Effect achieved:

If you want the text below the icon, change it to drawabletop.

2、 Inherit the button of the system and redraw it

Then the call is placed in the layout file.

Note that when called in XML file, layout_ Width and layout_ The two attributes of height must not be wrapped_ Content, otherwise the button will display only the text part.

3、 Inherit layout file

It is found that a button with text and icon can actually be regarded as a linear layout or relative layout, so it can be implemented by inheriting the layout.

First implement a button layout file img_ text_ bt.xml:

Then inherit the relativelayout layout:

It can then be invoked in the required XML file:

Then use in activity:

The final operation effect of three different methods:

Click here to download the complete example code.

I hope this article will be helpful to your Android program design.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>