android學習之三:如何使用自訂色彩

來源:互聯網
上載者:User

 1.color.xml裡面定義顏色

<?xml version="1.0" encoding="utf-8"?><resources>  <drawable name="darkgray">#808080FF</drawable>  <drawable name="white">#FFFFFFFF</drawable><drawable name="bule">#0000FF</drawable></resources>

 

2.在main.xml裡面使用,通過工程運行直接顯示

 

<AbsoluteLayout 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:autoLink="all"    android:text="百度:http://www.baidu.com"    android:layout_x="10px"    android:layout_y="100px"    /><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="百度:http://www.baidu.com"    android:textColor="@drawable/darkgray"    android:background="@drawable/white"    android:layout_x="10px"    android:layout_y="200px"    /></AbsoluteLayout>

3.如何通過程式時間來改變字型的顏色

 

Resources resource=this.getBaseContext().getResources();//this.getResources();        Drawable drawable=resource.getDrawable(R.drawable.white);                        TextView tw=(TextView) this.findViewById(R.id.text);        tw.setBackgroundDrawable(drawable);       /* tw.setBackgroundColor(Color.WHITE);        tw.setBackgroundResource(R.drawable.white);*/

 

總結:1.xml裡面是配置自訂色彩的格式
<resources>
  <drawable name="顏色名">#顏色值</drawable>
</resources>
2.在xml裡面引用
<TextView 
     ....................
    android:textColor="@drawable/darkgray"
    android:background="@drawable/white"
    .....................
    />

在根據字串的引用:
android:tex="@string/字串名" 

xml定義
strings.xml
<resources>
    <string name="字串名稱">字串</string>
</resources>
可以發現,這些資源的定義格式和引用的區別,只有xml檔案中元素不同而已,
在用的時候不需要去調用檔案名稱,因為這些元素直接R檔案中以ID的形式出現了

3.在程式中引用
Drawable drawable=resource.getDrawable(R.drawable.white);
 tw.setBackgroundDrawable(drawable);

4.這裡關於設定TextView的背景顏色的三種方法
  void setBackgroundColor(int color);
       void setBackgroundDrawable(Drawable d);
       void setBackgroundResource(int resid);
------------------------------------------------
 tw.setBackgroundDrawable(drawable);//這裡是引用自訂的資源檔
      tw.setBackgroundColor(Color.WHITE);//這裡則直接用Color類裡面的值
      tw.setBackgroundResource(R.drawable.white);//這裡則以ID的形式調用

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.