Android-control UI and androidui

Source: Internet
Author: User

Android-control UI and androidui

I. use XML to layout the file control UI

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 the prompt text, and a start game button is displayed in the center of the form.

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>

It is used to specify the application style and the text size and color.

Finally, the main activity is MainActivity. Apply the code to specify the layout file of the application.

setContentView(R.layout.activity_main);

2. Use code to control the UI

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; I Mport 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 the frame layout manager frameLayout. setBackgroundDrawable (this. getResources (). getDrawable (R. dra Wable. background); // sets the background image setContentView (frameLayout); // sets the display of frameLayout in the Activity // creates a TextView component TextView text1 = new TextView (this ); text1.setText ("control UI in Code"); text1.setTextSize (TypedValue. COMPLEX_UNIT_PX, 24); // set the text size, in the unit of pixel 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 text size in pixels text2.setTextColor (Color. rgb (1, 1, 1); LayoutParams = new LayoutParams (ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT); // creates an object params that saves layout parameters. 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. Proceed with caution. Are you sure you 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, "quit the game", Toast. LENGTH_LONG ). show (); finish ();}}). show () ;}}); frameLayout. addView (text2 );}}

And then update .....


Q: How can I control the UI of the android code? The following is an unexpected termination after I write it myself.

Paste logcat out. If you do not set the length and width of LinearLayout, an error is returned.
 
In Android development and Java code control UI, what is the error in this code?

I'm going, are you all wrong? There is also the import package, ctrl + shift + O, import the package, and then change the class name. Your file name is xghgh ,,,, but you wrote codeview... I have never learned java?
 

Related Article

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.