Preface to custom Android buttons with images
Now, the buttons on mobile devices are designed with big icons and small text. I hope that users can see what the buttons are for as soon as they see them, but they need necessary text prompts, the most common one is the number search button. The above is a large magnifier icon, and the following two words -- search.
Bill is also working on buttons with this effect recently. The process is always tortuous, but the results are always good ~ Now, Bill shares his practices with you, hoping to help friends who are not yet familiar with it.
Let's take a look at the twists and turns of Bill. Maybe there is your shadow in it:
At first, I thought it would be better to simply use the android imagebutton control. imagebutton can only display images and cannot add text to them.
So I thought of using the button directly, but the text of the button is displayed inside the image, and it cannot meet my needs. Give up.
There is always a way for a lazy person to be lazy. I can directly put the p-Good picture into imagebutton. This method is very simple and easy to use. However, what should we do once we need to change the text, or I want to display the text on the top of the image instead of the bottom? Although this method is simple, it lacks variability. Give up.
This is the so-called "one button, three folds ~
Is there a way to achieve both the button effect and the Custom button display?
The answer is yes. Next, Bill will explain the button creation process step by step.
Ideas
First, let's take a look at the implementation of this button. There is a way of thinking called "out of box", that is, to encourage everyone to jump out of a fixed mindset to seek new breakthroughs. Before jumping out of a box, we must first know what the box is.
Here, the box is "button ". We have been thinking about how to implement this "button", how to make the "button" display the image, and then show a row of words under the image. We are entangled in the box "button.
But in fact, when we find that the so-called "button" is actually a view, everything becomes simple.
It is just a view that allows you to click, set listeners, and display text or images. Then we will jump out of the box set for us by Android, and re-create a view with the features and appearances we need. Isn't that OK?
After analysis, the above button effect is actually a layout, the simplest vertical linear layout, the upper part is an imageview, and the lower part is a textview, click and set the listener for this layout.
First, we need to write our own imagebutton class, then write the layout for our custom button in the main layout file, and finally call our custom imagebutton in the activity.
Then we will implement this simple linearlayout together.
Encode your own imagebutton
Before writing our own imagebutton, if you are not sure how to dynamically load sub-la s in a static XML layout file, read the following blog post (this article is concise, I have already written it clearly, so bill will not repeat it)
Http://blog.csdn.net/lzx_bupt/article/details/5600187
First, we compile a myimagebutton class that inherits from linearlayout.
- Package com. billhoo. study;
-
- Import Android. content. context;
- Import Android. widget. imageview;
- Import Android. widget. linearlayout;
- Import Android. widget. textview;
-
- // Customize imagebutton, simulate imagebutton, and display text below it
- // Some buttons are provided.
- Public class myimagebutton extends linearlayout {
-
- Public myimagebutton (context, int imageresid, int textresid ){
- Super (context );
-
- Mbuttonimage = new imageview (context );
- Mbuttontext = new textview (context );
-
- Setimageresource (imageresid );
- Mbuttonimage. setpadding (0, 0, 0, 0 );
-
- Settext (textresid );
- Settextcolor (0xff000000 );
- Mbuttontext. setpadding (0, 0, 0, 0 );
-
- // Set attributes of the layout
- Setclickable (true); // clickable
- Setfocusable (true); // focused
- Setbackgroundresource (Android. R. drawable. btn_default); // the background of the normal button is used for layout.
- Setorientation (linearlayout. Vertical); // vertical layout
-
- // Add an image before adding text
- // Adding the order will affect the layout effect.
- Addview (mbuttonimage );
- Addview (mbuttontext );
- }
-
- // ---------------- Public method -----------------------------
- /*
- * Setimageresource Method
- */
- Public void setimageresource (INT resid ){
- Mbuttonimage. setimageresource (resid );
- }
-
- /*
- * Settext Method
- */
- Public void settext (INT resid ){
- Mbuttontext. settext (resid );
- }
-
- Public void settext (charsequence buttontext ){
- Mbuttontext. settext (buttontext );
- }
-
- /*
- * Settextcolor Method
- */
- Public void settextcolor (INT color ){
- Mbuttontext. settextcolor (color );
- }
-
- // ---------------- Private attribute -----------------------------
- Private imageview mbuttonimage = NULL;
- Private textview mbuttontext = NULL;
- }
Then write the XML layout for the Custom button in the main layout. Note that our "button" is actually a linear layout. Therefore, linearlayout should be written in XML instead of button or imagebutton.
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Orientation = "vertical" Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
-
- <! -- This is our "Data Management button" -->
- <Linearlayout Android: Id = "@ + ID/ll_bt_data_config"
- Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/>
-
- </Linearlayout>
Finally, set the listener for the Custom button in the activity.
- Package com. billhoo. study;
-
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. View. view;
- Import Android. widget. Button;
- Import Android. widget. linearlayout;
-
- Public class testactivity extends activity {
- /** Called when the activity is first created .*/
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
-
- Btdataconfig = new myimagebutton (this, R. drawable. Option, R. String. Text );
-
- // Get the container of the package button
- Llbtdataconfig = (linearlayout) findviewbyid (R. Id. ll_bt_data_config );
-
- // Add our custom button to this container
- Llbtdataconfig. addview (btdataconfig );
-
- // Set button listening
- Btdataconfig. setonclicklistener (New button. onclicklistener (){
- @ Override
- Public void onclick (view v ){
- Btdataconfig. settext ("the button has been clicked ");
- }
- });
- }
-
- Private linearlayout llbtdataconfig = NULL; // the container that wraps this button in the main layout
- Private myimagebutton btdataconfig = NULL;
- }
Effect
Extension
You can also extend the functions of this class by yourself, such as setting the surround position and size control of text, etc.
This article is from the "bill_hoo" blog, please be sure to keep this source http://billhoo.blog.51cto.com/2337751/772442