Many times, the commonly used controls in android cannot meet our needs, so we need to customize a control. Today, I made an example of a custom control to share with you.
First, define a layout implementation button internal layout:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "horizontal">
<ImageView
Android: id = "@ + id/imageView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_vertical"
Android: paddingBottom = "5dip"
Android: paddingLeft = "40dip"
Android: paddingTop = "5dip"
Android: src = "@ drawable/right_icon"/>
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_vertical"
Android: layout_marginLeft = "8dip"
Android: text = "OK"
Android: textColor = "#000000" type = "codeph" text = "/codeph"/>
</LinearLayout>
Next, write a class to inherit LinearLayout, import the layout just now, and set the required method so that the display of the custom control content can be controlled in the code.
Public class ImageBtn extends LinearLayout {
Private ImageView imageView;
Private TextView textView;
Public ImageBtn (Context context ){
Super (context );
// TODO Auto-generated constructor stub
}
Public ImageBtn (Context context, AttributeSet attrs ){
Super (context, attrs );
// TODO Auto-generated constructor stub
LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
Inflater. inflate (R. layout. imagebtn, this );
ImageView = (ImageView) findViewById (R. id. imageView1 );
TextView = (TextView) findViewById (R. id. textView1 );
}
/**
* Set image resources
*/
Public void setImageResource (int resId ){
ImageView. setImageResource (resId );
}
/**
* Set the displayed text.
*/
Public void setTextViewText (String text ){
TextView. setText (text );
}
}
Add this control to layout of the custom control. You only need to add it to xml.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "horizontal">
<Cn.com. karl. view. ImageBtn
Android: id = "@ + id/btn_right"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: background = "@ drawable/btn"
/>
<Cn.com. karl. view. ImageBtn
Android: id = "@ + id/btn_error"
Android: layout_marginLeft = "5dp"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: background = "@ drawable/btn"
/>
</LinearLayout>
The background image is used in drawable/btn. xml.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: state_focused = "true" android: state_pressed = "false" android: drawable = "@ drawable/btn_normal"> </item>
<Item android: state_pressed = "true" android: drawable = "@ drawable/btn_white"> </item>
<Item android: state_checked = "true" android: drawable = "@ drawable/btn_white"> </item>
<Item android: state_focused = "false" android: state_pressed = "false" android: drawable = "@ drawable/btn_normal"> </item>
Finally, set the control in activity, which is similar to other controls:
Public class IdentifyButtonActivity extends Activity {
Private ImageBtn imageBtn1;
Private ImageBtn imageBtn2;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. identifybutton );
ImageBtn1 = (ImageBtn) this. findViewById (R. id. btn_right );
ImageBtn2 = (ImageBtn) this. findViewById (R. id. btn_error );
ImageBtn1.setTextViewText ("OK ");
ImageBtn2.setTextViewText ("cancel ");
ImageBtn1.setImageResource (R. drawable. right_icon );
ImageBtn2.setImageResource (R. drawable. error_icon );
ImageBtn1.setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Toast. makeText (getApplicationContext (), "click the correct button", 1). show ();
}
});
ImageBtn2.setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Toast. makeText (getApplicationContext (), "click error button", 1). show ();
}
});
}
}
Public class IdentifyButtonActivity extends Activity {
Private ImageBtn imageBtn1;
Private ImageBtn imageBtn2;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. identifybutton );
ImageBtn1 = (ImageBtn) this. findViewById (R. id. btn_right );
ImageBtn2 = (ImageBtn) this. findViewById (R. id. btn_error );
ImageBtn1.setTextViewText ("OK ");
ImageBtn2.setTextViewText ("cancel ");
ImageBtn1.setImageResource (R. drawable. right_icon );
ImageBtn2.setImageResource (R. drawable. error_icon );
ImageBtn1.setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Toast. makeText (getApplicationContext (), "click the correct button", 1). show ();
}
});
ImageBtn2.setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Toast. makeText (getApplicationContext (), "click error button", 1). show ();
}
});
}
}
Finally, let's take a look at the effects of our custom controls!
After clicking the button, you can also press the button.
From the wangkuifeng0118 Column