應(yīng)用開發(fā)筆記

          2011年05月9日

          android 各種控件顏色值的設(shè)置(使用Drawable,Color)

          Filed under: Android開發(fā) — 標(biāo)簽:, , , , , — admin @ 16:21

          在Android中,如果需要改變控件默認(rèn)的顏色,包括值的顏色,需要預(yù)先在strings.xml中設(shè)置,類似字符串,可以反復(fù)調(diào)用。Android中顏色可以使用drawable或是color來定義。
          本例中strings.xml內(nèi)容:

          1 2 3 4 5 6 7 8 9 
          <?xml version="1.0" encoding="utf-8"?> <resources>     <string name="hello">Hello World, Main!</string>     <string name="app_name">Color</string>     <drawable name="red">#ff0000</drawable>     <color name="gray">#999999</color>     <color name="blue">#0000ff</color>     <color name="background">#ffffff</color> </resources>

          上面定義了幾個顏色值,下面是在布局文件中的調(diào)用,main.xml內(nèi)容:

          1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
          <?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"     android:background="@color/background"     > <TextView  android:id="@+id/tv1"     android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="@string/hello"     android:textColor="@drawable/red"     /> <TextView  android:id="@+id/tv2"     android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="@string/hello"     android:textColor="@color/gray"     /> <TextView  android:id="@+id/tv3"     android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="@string/hello"     /> </LinearLayout>

          在Java程序中使用:

          1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 
          package com.pocketdigi.color;   import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.TextView;   public class Main extends Activity {     /** Called when the activity is first created. */ 	TextView tv1,tv2,tv3;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         tv1=(TextView)findViewById(R.id.tv1);         tv2=(TextView)findViewById(R.id.tv2);         tv3=(TextView)findViewById(R.id.tv3);         tv3.setTextColor(Color.BLUE);//直接使用android.graphics.Color的靜態(tài)變量         tv2.setTextColor(this.getResources().getColor(R.color.blue));//使用預(yù)先設(shè)置的顏色值       } }

          這里以TextView為例,其他控件類似.

          posted on 2013-03-05 15:45 姚先進(jìn) 閱讀(233) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 全椒县| 平陆县| 靖江市| 廉江市| 灵璧县| 佛教| 昌都县| 庆安县| 铅山县| 泾阳县| 城固县| 合作市| 东乡| 庄浪县| 永安市| 和林格尔县| 德钦县| 合川市| 陆丰市| 姚安县| 东源县| 南通市| 达尔| 陆河县| 木兰县| 姚安县| 海林市| 仁寿县| 灵寿县| 资兴市| 阿克陶县| 普定县| 香河县| 孝昌县| 洛隆县| 南涧| 台东县| 宁阳县| 京山县| 临夏市| 江达县|