標籤:android
,點擊按鈕就會切換畫面的顏色
首先,先建立一個安卓項目(我的版本是4.4.2的),名字為"world",當然,也可以別的名稱
然後在res檔案夾下找到layout檔案夾,找到activity_main.xml或fragment_main.xml,在裡面輸入或拖拽按鈕
<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:paddingBottom="@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:text="@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="紅色" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="44dp" android:text="藍色" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button2" android:layout_below="@+id/button2" android:layout_marginTop="51dp" android:text="綠色" /></RelativeLayout>
然後在values檔案夾下建立一個color.xml
<?xml version="1.0" encoding="utf-8"?><resources> <color name="red">#FF0000</color> <color name="green">#008000</color> <color name="blue">#0000FF</color></resources>
最後在src下的java檔案裡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);//點擊事件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;}}
最後運行,點擊相應的按鈕就會出現以下效果了: