button, which is one of the most applied components of Android, has two uses, one for XML and one for direct use in the program.
In the XML layout file, you will encounter some of the following units
PX: Is the pixel point of the screen
In: Inch
MM: MM
PT: lbs, 1/72 inch
DP: An abstract unit based on density, if a 160dpi screen, 1dp=1px
Dip: Equivalent to DP
SP: similar to DP, but also scaled according to the user's font size preference.
It is recommended to use SP as the unit of text, other dip
Example 1: Layout in XML, which makes it easier to set up controls later and conforms to the MVC pattern
Main.xml Layout file
<?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_pare NT "> <textview android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android : text= "This is a button example"/> <button android:layout_width= "fill_parent"------full parent control Android:layout_ height= "wrap_content"------full content android:id= "@+id/button1"----Set the button's ID android:text= "button One"------settings by The text of the button can also be displayed with a string/> <button android:layout_width= "150dip"---the width of the button two android:layout_height= "30d IP "---button two height android:background=" #aa00aa "---set button background color android:textcolor=" #00aa00 "---Set the text color of button two Android:layout_gravity= Center---The settings control is centered, note: android:gravity= "center" table is text centered in the control android:id= "@+id/button2" android:text= "button two"/> ≪/linearlayout>
Testbutton.java Source Code
Package Org.loulijun.button;import Android.app.activity;import Android.os.bundle;import android.view.Gravity; Import Android.view.view;import Android.widget.button;import Android.widget.toast;public class TestButton extends Activity {/** Called when the activity is first created. */private Button btn1,btn2; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); btn1= (Button) Findviewbyid (r.id.button1);---Get the parent control ID btn2= (button) Findviewbyid (R.id.button2); Set the listener for the control, when the button is clicked, a prompt pops up, when you click the button two, exit the program Btn1.setonclicklistener (new Button.onclicklistener () { @Override public void OnClick (View arg0) {//TODO Auto-gene Rated method Stub Toast toast=toast.maketext (Testbutton.this, "You clicked the button" +btn1.gettext (). tostr ING (), toast.length_short); Toast.setgravity (gravity.top,0,150); Toast.show (); } }); Btn2.setonclicklistener (New Button.onclicklistener () {@Override Publ IC void OnClick (View v) {//TODO auto-generated method stub TestButton.this.finish (); } }); }}
Operation Result:
Of course, you can also use the corresponding method directly in the program to this is the corresponding property of the button
public class Testbutton extends activity{ private Button btn1,btn2; public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); btn1= (Button) Findviewbyid (r.id.button1); Btn2= (Button) Findviewbyid (r.id.button2); Btn1.setwidth (150);--Set the width of the button, of course, you can also use SetHeight () to set the height of btn2.setwidth (); Btn1.settext ("button One");---set button text display information btn2.settext ("Anne II"); Btn1.settextcolor (Color.green);---Set the text color in the button Btn2.settextcolor (color.red); Btn1.settextsize (+);---Set the text size in the button btn2.settextsize (); Btn2.setbackgroundcolor (color.red);---Set button background color } }
The above is the Android UI control Series: button content, more about topic.alibabacloud.com (www.php.cn)!