您的位置:首页 > 移动开发 > Android开发

Android中帧布局FrameLayout 霓虹灯效果 color数组报错问题

2016-08-05 15:43 405 查看
《疯狂Android讲义》中 用帧布局FrameLayout  来制造 霓虹灯效果,布局文件activity.xml文件没有问题,但是在编写java程序的MainActivity.java文件的时候,写到color数组的时候,总是显示红色的错号,但是始终不知道如何的解决这些错号,这里先把MainActivity.java代码文件贴出来,如下所示

package com.example.framelayout;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

private int currentColor=0;
//定义一个颜色数组,编到这里的时候几乎每一行的color处都会显示红色的错号,百思不得其解???????????
//按常理,既然用到了R.color.color1,则gen/R.java中应该有color这样的一个类,类中有color1到color6这些常量
//但是始终找不到,这是为什么呢,想办法往R.java里边加color,没法加,因为这个文件本身就是自己自动生成的
final int[] colors = new int[]{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
};

final int[] names = new int[]{
R.id.view01,
R.id.view02,
R.id.view03,
R.id.view04,
R.id.view05,
R.id.view06
};

TextView[] views = new TextView[names.length];
Handler handler = new Handler(){
public void handleMessage(Message msg){

//表明消息来自本程序发送
if(msg.what == 0x123){
for(int i=0;i<names.length;i++){
views[i].setBackgroundResource(colors[(i+currentColor)%names.length]);
}
currentColor++;
}
super.handleMessage(msg);
}
};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<names.length;i++){
views[i]=(TextView) findViewById(names[i]);
}
//定义一个线程周期性的改变currentcolor变量值
new Timer().schedule(new TimerTask(){
public void run(){
handler.sendEmptyMessage(0x123);
}
},0,200);
}

}

定义一个颜色数组,编到这里的时候几乎每一行的color处都会显示红色的错号,百思不得其解???????????

按照常理来讲,既然用到了R.color.color1,则在gen/R.java文件当中,必定会有color这样的一个类,类中有color1到color6这些常量但是始终找不到,这是为什么呢,想办法往R.java里边加color,没法加,因为这个文件本身就是自己自动生成的。那就想办法在其他的地方造一个关于color的文件,该文件中有上述java程序中的数组中所对应的6中颜色,于是就在res/values目录下加入一个color.xml文件,文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color1">#f00</color>
<color name="color2">#0f0</color>
<color name="color3">#00f</color>
<color name="color4">#ff0</color>
<color name="color5">#f0f</color>
<color name="color6">#0ff</color>
</resources>
<span style="font-size:18px;color:#ff0000;"><strong>布局文件activity.xml中的程序如下</strong></span>
<pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<!-- 依次定义6个TextView,先定义的TextView位于底层,后定义的TextView位于上层 -->
<TextView
android:id="@+id/view01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="320px"
android:height="320px"
android:background="#f00"
/>

<TextView
android:id="@+id/view02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="280px"
android:height="280px"
android:background="#0f0"
/>

<TextView
android:id="@+id/view03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="240px"
android:height="240px"
android:background="#00f"
/>

<TextView
android:id="@+id/view04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="200px"
android:height="200px"
android:background="#ff0"
/>

<TextView
android:id="@+id/view05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="160px"
android:height="160px"
android:background="#f0f"
/>

<TextView
android:id="@+id/view06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:width="120px"
android:height="120px"
android:background="#0ff"
/>

</FrameLayout>


<strong><span style="font-size:18px;color:#ff0000;">完事之后,再去看gen/R.java程序,可以看到,里边出现了color类,类中有6个不同的颜色变量,具体的R.java代码如下:</span></strong>
<pre class="java" name="code">/* AUTO-GENERATED FILE.  DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found.  It
* should not be modified by hand.
*/

package com.example.framelayout;

public final class R {
public static final class attr {
}
public static final class color {
public static final int color1=0x7f040000;
public static final int color2=0x7f040001;
public static final int color3=0x7f040002;
public static final int color4=0x7f040003;
public static final int color5=0x7f040004;
public static final int color6=0x7f040005;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int menu_settings=0x7f080006;
public static final int view01=0x7f080000;
public static final int view02=0x7f080001;
public static final int view03=0x7f080002;
public static final int view04=0x7f080003;
public static final int view05=0x7f080004;
public static final int view06=0x7f080005;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int activity_main=0x7f070000;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050001;
public static final int menu_settings=0x7f050002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.

Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.

API 11 theme customizations can go here.

Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.

API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/**  Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息