android--Control UI Interface

Source: Internet
Author: User

I. Using an XML layout file to control the UI interface

The Res\layout\activity_main.xml code is as follows:

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/framelayout1 "    android:layout_width=" Match_parent    " android:layout_height= "Match_parent"    android:background= "@drawable/background" ><textview     Android : layout_width= "match_parent"    android:layout_height= "wrap_content" android:text= "@string/title" style= "@style /text "    ></textview><textview    android:id=" @+id/startbutton "android:layout_gravity=" Center_ Vertical|center_horizontal "android:text=" @string/start "android:layout_width=" Wrap_content "Android:layout_ height= "Wrap_content" style= "@style/text"    >    </TextView></FrameLayout>

A TextView component is used to display prompt text, and a Start Game button appears in the middle of the form

Where the Res\values\styles.xml code is as follows

<resources xmlns:android= "http://schemas.android.com/apk/res/android" ><style name= "text" >    < Item Name= "Android:textsize" >24dp</item>    <item name= "Android:textcolor" > #111111 </item> </style></resources>

Specifies the style of the app, specifying the size and color of the text.

Finally, in the main activity, the mainactivity, apply the code to specify the layout file for the app.

Setcontentview (R.layout.activity_main);

Second, using code to control the UI interface

Package Com.basil_lee.ui;import Android.os.bundle;import Android.app.actionbar.layoutparams;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.graphics.color;import Android.util.typedvalue;import Android.view.gravity;import Android.view.Menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.framelayout;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);// Setcontentview (R.layout.activity_main); Framelayout framelayout=new Framelayout (this);//Create Frame Layout manager framelayout.setbackgrounddrawable (This.getresources (). Getdrawable (R.drawable.background));//Set background picture Setcontentview (framelayout);//set to show framelayout//in activity Create a TextView component TextView text1=new TextView (This), Text1.settext ("Control UI Interface in Code"), Text1.settextsize (Typedvalue.complex_unit_px,24);//Set the size of the text in pixels text1.settextcolor (Color.rgb (1, 1, 1)); Framelayout.addview (Text1);// Create another TextView component TextView text2=new TextView (this); Text2.settext ("Click to enter the game ... "); Text2.settextsize (typedvalue.complex_unit_px,50);//Set the size of the text in pixels text2.settextcolor (Color.rgb (1, 1, 1)); Layoutparams params=new Layoutparams (viewgroup.layoutparams.wrap_content,viewgroup.layoutparams.wrap_content);// Create an object that holds layout parameters params.gravity=gravity.center_horizontal| Gravity.center_vertical;text2.setlayoutparams (params);//Add a click event for the Text2 component and add the component to the Layout manager Text2.setonclicklistener ( New Onclicklistener () {public void OnClick (View arg0) {new Alertdialog.builder (mainactivity.this). Settitle ("System Prompt"). Setmessage ("The game is risky, enter need to be cautious, really want to enter?") "). Setpositivebutton (" OK ", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface arg0, int arg1) { Toast.maketext (Mainactivity.this, "Enter the game", Toast.length_long). Show ();}}). Setnegativebutton ("Exit", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface arg0, int arg1){Toast.maketext (Mainactivity.this, "Exit Game", Toast.length_long). Show (); Finish ();}}).   Show ();}}); Framelayout.addview (Text2);}}

Continue to update later .....

android--Control UI Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.