BUTTON class structure:
From the class structure diagram, we can see that the Button is inherited from TextView, so some attributes of TextView also apply to the Button control.
ImageButton class structure:
ImageButton uses an icon to represent some text. It does not have the Android: text attribute. It specifies the icon position by Android: src
Android: src = "@ drawable/back_48"
Other attributes are similar to buttons.
1. How do I set the button style?
Set Through Android: background
[Java]
<Button android: id = "@ + id/myBtn1" android: text = "Button 1 sets the background style"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: background = "# fff000"/>
2. How to set the background icon:
[Java]
<Button android: id = "@ + id/myBtn6" android: text = "Button 6 sets the background icon"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
Android: textStyle = "bold" android: background = "@ drawable/back_48"
/>
3. How to set the text color of the button
Android: textColor
[Java]
<Button android: id = "@ + id/myBtn2" android: text = "Button 2 font color"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: textColor = "# ff0000"/>
Nbsp;
4. How to set the button text style
Android: textStyle
[Java]
<Button android: id = "@ + id/myBtn3" android: text = "Button 3 font bold"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: textColor = "# ff0000" android: textStyle = "bold"/>
5. How to add a listener registration event for the button
Method 1: setOnClickListener
[Java]
MyBtn4.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
MyBtn4.setText ("setOnclickListener event listener registration successful ");
}
});
Practice:
1. How to set the icon position of ImageButton
[Html]
<ImageButton android: id = "@ + id/imgBtn01" android: src = "@ drawable/forward_48"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"/>
2. How to add a listener to register an event for ImageButton?
Like Button, it can be set in two ways.
Method 1: Use onClickListener
[Java]
ImgBtn01.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
MyTextView. setText ("ImageButton listener event ");
}
});
Method 2: Set Through XML file
[Html]
<ImageButton android: id = "@ + id/imgBtn02" android: src = "@ drawable/back_48"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
Android: onClick = "ImageButtonXml"/>
1. Set transparency
[Java]
ImgBtn01.setAlpha (50); // sets the transparency.
Author: hjm4702192