, click the button to toggle the color of the screen
First of all, create an Android project (my version is 4.4.2), the name "World", of course, can also be another name
Then find the Layout folder under the Res folder, find Activity_main.xml or fragment_main.xml, enter or drag the button
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Android:id = "@+id/layout" tools:context= "com.example.world.mainactivity$placeholderfragment" > <TextView android:id = "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:te xt= "@string/hello_world"/> <button android:id= "@+id/button1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_below= "@+id/textview1" android:layout_marginleft= " 14DP "android:layout_margintop=" 22DP "Android:layout_torightof= "@+id/textview1" android:text= "Red"/> <button android:id= "@+id/button2" a Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignright= "@+id/butt On1 "android:layout_below=" @+id/button1 "android:layout_margintop=" 44DP "android:text=" Blue "/> & Lt Button android:id= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "android:layout_alignright=" @+id/button2 "android:layout_below=" @+id/button2 "Android:layout_marg intop= "51DP" android:text= "green"/></relativelayout>
Then create a color.xml under the values folder
<?xml version= "1.0" encoding= "Utf-8"?><resources> <color name= "Red" > #FF0000 </color> <color name= "green" > #008000 </color> <color name= "Blue" > #0000FF </color></ Resources>
Finally, in the Java file under SRC Mainactivity.java
Package Com.example.world;import Android.app.activity;import Android.app.actionbar;import android.app.Fragment; Import Android.os.bundle;import android.text.layout;import android.view.layoutinflater;import android.view.Menu; Import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.relativelayout;import Android.os.Build; public class Mainactivity<activitylayout> extends Activity {private Button btn1,btn2,btn3;private relativelayout Layout, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.fragment_main); layout= (relativelayout) This.findviewbyid (r.id.layout); btn1= (Button) This.findviewbyid (R.id.button1);//Click event Btn1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick ( View v) {Layout.setbackgroundcolor (Getresources (). GetColor (r.color.red));}); Btn2= (Button) This.findviewbyid (R.id.buttOn2); Btn2.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {Layout.setbackgroundcolor ( Getresources (). GetColor (R.color.blue));}); btn3= (Button) This.findviewbyid (R.id.button3); Btn3.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {Layout.setbackgroundcolor (Getresources (). GetColor (R.color.green));}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Last run, click on the corresponding button will appear the following effect: