|
Android Custom button
1. Define the button layout file
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android650) this.width=650; "src=" http://techfoxbbs.com/static/image/smiley/default/shocked.gif "border=" 0 "/> rientation= "Horizontal"
android:layout_gravity= "Center"
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
>
<imageview
Android:id= "@+id/iconmoney"
Android:layout_width= "25DP"
android:layout_height= "Fill_parent"
>
</ImageView>
<textview
Android:id= "@+id/nummeoney"
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:gravity= "Center"
android:text= "4444"
Android:textsize= "20DP"
android:layout_margintop= "0DP"
Android:layout_marginbottom= "3DP"
android:layout_marginleft= "8DP"
>
</TextView>
<imageview
android:layout_marginleft= "5DP"
Android:id= "@+id/iconadd"
Android:layout_width= "25DP"
android:layout_height= "Fill_parent"
>
</ImageView>
</LinearLayout>
2 "Inherit layout file
package Com.widget ;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.util.AttributeSet;
Import Android.view.LayoutInflater;
Import Android.widget.ImageView;
Import android.widget.LinearLayout;
Import Android.widget.TextView;
Import COM.DREAMPLANEGAMES.R;
public class Moneyview extends LinearLayout {
Private ImageView Iconmoney;
Private TextView TextView;
Private ImageView Addmoney;
Public Moneyview (Context context,attributeset attributeset) {
Super (context, attributeset);
Layoutinflater.from (context). Inflate (R.layout.money, this,true);//Specify Layout
This.iconmoney = (ImageView) Findviewbyid (R.id.iconmoney);
This.textview = (TextView) Findviewbyid (R.id.nummeoney);
This. addmoney= (ImageView) Findviewbyid (R.id.iconadd);
This.setclickable (TRUE);//Can be clicked
This.setfocusable (TRUE);
}
Setting the contents of a control
public void SetText (String text) {
This.textView.setText (text);
}
public void SetTextColor (int color) {
This.textView.setTextColor (color);
}
public void settextsize (float size) {
This.textView.setTextSize (size);
}
public void setimg (Bitmap img1,bitmap img2) {
This.iconMoney.setImageBitmap (IMG1);
This. Addmoney.setimagebitmap (IMG2);
This.yes.setImageBitmap (IMG2);
}
}
3 "Call to control
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android650) this.width=650; "src=" http://techfoxbbs.com/static/image/smiley/default/shocked.gif "border=" 0 "/> rientation= "Horizontal"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
>
<imageview android:id= "@+id/mainmenuimage"
android:src= "@drawable/xk_bg2"
Android:scaletype= "Fitxy"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
</ImageView> //Call a custom control
<com.widget.moneyview--------------------class Name
Android:layout_width= "Wrap_content"
android:layout_height= "30DP"
Android:id= "@+id/btnmoney"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"
android:layout_margintop= "20DP"
android:layout_marginleft= "20DP"
>
</com.widget.MoneyView>
</RelativeLayout>
4 "to the corresponding activity call
public class Sfmainmenu extends Activity implements View.onclicklistener
{
Public Moneyview Moneyview;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Set Full Screen
GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Remove Application Title
This.requestwindowfeature (Window.feature_no_title);
Setting up a vertical screen
Setrequestedorientation (activityinfo.screen_orientation_portrait);
Setcontentview (R.layout.main);
Get the custom button
moneyview= (Moneyview) Findviewbyid (R.id.btnmoney);
Invoking a function of a custom control to set the contents of a control
Moneyview.setimg (Bitmapfactory.decoderesource (Getresources (), R.drawable.money), Bitmapfactory.decoderesource ( Getresources (), r.drawable.add));
Moneyview.settext ("+mypointbalance");
Custom button Response Events
Moneyview.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
}
});
}
Return key
@Override
public void onbackpressed () {
Super.onbackpressed ();
}
}
|