android初级三

上代码:实现textview文字颜色变化

  1 import android.os.Bundle;
  2 import android.app.Activity;
  3 import android.content.res.Resources;
  4 import android.view.Menu;
  5 import android.widget.TextView;
  6 import android.widget.LinearLayout;
  7 import android.graphics.Color;
  8 import android.graphics.drawable.Drawable;
  9 public class MainActivity extends Activity {
 10     private LinearLayout myLayout;
 11     private LinearLayout.LayoutParams layoutP;
 12     private int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
 13     private TextView black_TV, blue_TV, cyan_TV, dkgray_TV,
 14     gray_TV, green_TV,ltgray_TV, magenta_TV, red_TV,
 15     transparent_TV, white_TV, yellow_TV;
 16     @Override
 17     protected void onCreate(Bundle savedInstanceState) {
 18         super.onCreate(savedInstanceState);
 19         setContentView(R.layout.activity_main);
 20         TextView tv = new TextView(this);
 21         tv.setText("这是一个测试Android的helloWorld");
 22      //   setContentView();
 23         /* 实例化一个LinearLayout布局对象*/
 24         myLayout = new LinearLayout(this);
 25         /* 设置LinearLayout的布局为垂直布局*/
 26         myLayout.setOrientation(LinearLayout.VERTICAL);
 27         /* 设置LinearLayout布局背景图片*/
 28         myLayout.setBackgroundResource(R.drawable.onea);
 29         /* 加载主屏布局*/
 30         setContentView(myLayout);
 31         /* 实例化一个LinearLayout布局参数,用来添加View */
 32         layoutP = new LinearLayout.LayoutParams(WC, WC);
 33         /* 构造实例化TextView对象*/
 34         constructTextView();
 35         /* 把TextView添加到LinearLayout布局中*/
 36         addTextView();
 37         /* 设置TextView文本颜色*/
 38         setTextViewColor();
 39         /* 设置TextView文本内容*/
 40         setTextViewText();
 41     }
 42     /* 设置TextView文 本内容
 43     */
 44     public void setTextViewText() {
 45     black_TV.setText("黑色");
 46     blue_TV.setText("蓝色");
 47     cyan_TV.setText("青绿色");
 48     dkgray_TV.setText("灰黑色");
 49     gray_TV.setText("灰色");
 50     green_TV.setText("绿色");
 51     ltgray_TV.setText("浅灰色");
 52     magenta_TV.setText("红紫色");
 53     red_TV.setText("红色");
 54     transparent_TV.setText("透明");
 55     white_TV.setText("白色");
 56     yellow_TV.setText("黄色");
 57     } 
 58     /* 设置TextView文本颜色 */
 59     public void setTextViewColor() {
 60         Resources myColor=getBaseContext().getResources();
 61         //getBaseContext()获得基础Context
 62         //getResources()获得资源
 63         Drawable color_M=myColor.getDrawable(R.color.lightgreen );
 64     black_TV.setBackgroundDrawable(color_M);
 65     blue_TV.setTextColor(Color.BLUE);
 66     dkgray_TV.setTextColor(Color.DKGRAY);
 67     gray_TV.setTextColor(Color.GRAY);
 68     green_TV.setTextColor(Color.GREEN);
 69     ltgray_TV.setTextColor(Color.LTGRAY);
 70     magenta_TV.setTextColor(Color.MAGENTA);
 71     red_TV.setTextColor(Color.RED);
 72     transparent_TV.setTextColor(Color.TRANSPARENT);
 73     white_TV.setTextColor(Color.WHITE);
 74     yellow_TV.setTextColor(Color.YELLOW);
 75     }
 76     public void constructTextView() {
 77         black_TV = new TextView(this);
 78         blue_TV = new TextView(this);
 79         cyan_TV = new TextView(this);
 80         dkgray_TV = new TextView(this);
 81         gray_TV = new TextView(this);
 82         green_TV = new TextView(this);
 83         ltgray_TV = new TextView(this);
 84         magenta_TV = new TextView(this);
 85         red_TV = new TextView(this);
 86         transparent_TV = new TextView(this);
 87         white_TV = new TextView(this);
 88         yellow_TV = new TextView(this);
 89         } /* 90         TextView添
 91  92  93         LinearLayout布
 94  95  96         */
 97         public void addTextView() {
 98         myLayout.addView(black_TV, layoutP);
 99         myLayout.addView(blue_TV, layoutP);
100         myLayout.addView(cyan_TV, layoutP);
101         myLayout.addView(dkgray_TV, layoutP);
102         myLayout.addView(gray_TV, layoutP);
103         myLayout.addView(green_TV, layoutP);
104         myLayout.addView(ltgray_TV, layoutP);
105         myLayout.addView(magenta_TV, layoutP);
106         myLayout.addView(red_TV, layoutP);
107         myLayout.addView(transparent_TV, layoutP);
108         myLayout.addView(white_TV, layoutP);
109         myLayout.addView(yellow_TV, layoutP);
110         }
111     @Override
112     public boolean onCreateOptionsMenu(Menu menu) {
113         // Inflate the menu; this adds items to the action bar if it is present.
114         getMenuInflater().inflate(R.menu.main, menu);
115         return true;
116     }
117     
118 }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:text="TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="这里使用Graphics颜色静态常量"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>

新加drawable.xml,其中添加一个white 颜色值

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <color name="white">#ffffffff</color>
4 </resources>

在代码中由ID 获取TextView

1 TextView text_A=(TextView)findViewById(R.id.TextView01);
2 TextView text_B=(TextView)findViewById(R.id.TextView02);

获取Resources 对象

1 Resources myColor_R=getBaseContext().getResources();
2 //getBaseContext()获得基础Context
3 //getResources()从Context 获取资源实例对象

获取Drawable 对象 

Drawable myColor_D=myColor_R.getDrawable(R.color.white);

设置文本背景颜色

text_A.setBackgroundDrawable(myColor_D);

利用android.graphics.Color 的颜色静态变量来改变文本颜色

text_A.setTextColor(android.graphics.Color.GREEN);

text_A.setTextColor(android.graphics.Color.GREEN);

text_A.setTextColor(android.graphics.Color.GREEN);

 

posted @ 2013-04-15 11:50  程序world  阅读(68)  评论(0)    收藏  举报