First, we compile a myimagebutton class that inherits from linearlayout.
Public class myphoneimagebutton: linearlayout {private imageview mbuttonimage = NULL; private textview mbuttontext = NULL; Public myphoneimagebutton (context): Base (context) {mbuttonimage = new imageview (context ); mbuttontext = new textview (context); mbuttonimage. setpadding (0, 0, 0, 0); mbuttontext. setpadding (0, 0, 0, 0); // set the attribute clickable = true for the current layout; focusable = true; addview (mbuttontext); addview (mbuttonimage );} public void setimageresource (INT resid) {mbuttonimage. setimageresource (resid);} public void settext (string resid) {mbuttontext. TEXT = resid;} public void settextcolor (INT color) {mbuttontext. settextcolor (resources. getcolor (color ));}}
View code
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.
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvPPhone" android:orientation="horizontal" />
View code
Finally, set the listener for the Custom button in the activity.
Private linearlayout llbtdataconfig = NULL; // Private myphoneimagebutton btdataconfig = NULL; btdataconfig = new myphoneimagebutton (this); btdataconfig. settextcolor (resource. color. text_color_projectinfo); btdataconfig. settext ("I Am a text"); btdataconfig. setimageresource (resource. drawable. phone); llbtdataconfig = findviewbyid <linearlayout> (resource. id. tvpphone); llbtdataconfig. addview (btdataconfig); llbtdataconfig. setonclicklistener (New llbtdataconfig_click (); // listener events inherit from public class llbtdataconfig_click: Java. lang. object, view. ionclicklistener {public void onclick (view v ){}}
View code
In addition, it is better to monitor click events.
llbtDataConfig.Click += (object sender, EventArgs e) => { llbtDataConfig_Click (); };