Android小程序?qū)崿F(xiàn)切換背景顏色
本文實(shí)例為大家分享了Android實(shí)現(xiàn)切換背景顏色的具體代碼,供大家參考,具體內(nèi)容如下
(1)首先打開(kāi)界面布局文件,添加兩個(gè)Button
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnYellow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="黃色" android:textColor="#fff" /> <Button android:id="@+id/btnBlue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="藍(lán)色" android:textColor="#fff" /> </LinearLayout>

(2)在res/values目錄下創(chuàng)建一個(gè)顏色資源文件color.xml


(3)編輯color.xml
<?xml version="1.0" encoding="UTF-8"?> <resources> <color name="yellow">#ffee55</color> <color name="blue">#0000ff</color> </resources>
(4)此時(shí)在R.java中自動(dòng)生成color資源

(5)最后編寫(xiě)程序代碼
package com.example.ch03;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
//聲明兩個(gè)按鈕
Button btnYellow;
Button btnBlue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//根據(jù)Id找到界面中的兩個(gè)按鈕組件
btnYellow=(Button)this.findViewById(R.id.btnYellow);
btnBlue=(Button)this.findViewById(R.id.btnBlue);
//注冊(cè)監(jiān)聽(tīng)器
btnYellow.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//設(shè)置背景顏色為黃色
getWindow().setBackgroundDrawableResource(R.color.yellow);
}
});
btnBlue.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//設(shè)置背景顏色為藍(lán)色
getWindow().setBackgroundDrawableResource(R.color.blue);
}
});
}
}
(6)結(jié)果展示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)給TableLayou繪制邊框的方法
這篇文章主要介紹了Android實(shí)現(xiàn)給TableLayou繪制邊框的方法,涉及Android TableLayou布局控制相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android ViewPager相冊(cè)橫向移動(dòng)的實(shí)現(xiàn)方法
本篇文章小編為大家介紹,Android ViewPager相冊(cè)橫向移動(dòng)的實(shí)現(xiàn)方法。需要的朋友參考下2013-04-04
Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android自定義ViewGroup實(shí)現(xiàn)右滑進(jìn)入詳情
這篇文章主要為大家詳細(xì)介紹了Android如何通過(guò)自定義ViewGroup實(shí)現(xiàn)右滑進(jìn)入詳情效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01
Android多點(diǎn)觸控實(shí)現(xiàn)對(duì)圖片放大縮小平移,慣性滑動(dòng)等功能
這篇文章主要介紹了Android多點(diǎn)觸控實(shí)現(xiàn)對(duì)圖片放大縮小平移,慣性滑動(dòng)等功能的相關(guān)資料,需要的朋友可以參考下2016-02-02
flutter實(shí)現(xiàn)底部不規(guī)則導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)底部不規(guī)則導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
詳解Android跨進(jìn)程IPC通信AIDL機(jī)制原理
本篇文章主要介紹了詳解Android跨進(jìn)程IPC通信AIDL機(jī)制原理,詳細(xì)的介紹了AIDL的概念和使用,具有一定的參考價(jià)值,有興趣的可以了解一下2018-01-01
Android利用RecyclerView編寫(xiě)聊天界面
這篇文章主要為大家詳細(xì)介紹了Android利用RecyclerView編寫(xiě)聊天界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

