标签: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>
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#FF0000</color> <color name="green">#008000</color> <color name="blue">#0000FF</color> </resources>
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; @Override protected 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() { @Override public void onClick(View v) { layout.setBackgroundColor(getResources().getColor(R.color.red)); } }); btn2=(Button) this.findViewById(R.id.button2); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { layout.setBackgroundColor(getResources().getColor(R.color.blue)); } }); btn3=(Button) this.findViewById(R.id.button3); btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { layout.setBackgroundColor(getResources().getColor(R.color.green)); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
初识安卓小程序(点击按钮切换屏幕颜色),布布扣,bubuko.com
标签:android
原文地址:http://blog.csdn.net/u011263966/article/details/25407889