Android 資源詳解(一) 顏色、字串、位元影像資源

來源:互聯網
上載者:User

標籤:android   位元影像   

                            顏色資源

顏色值的定義是通過 RGB 三原色和一個 alpha 值來定義的。顏色值定義的開始是一個

井號(刑,後面是 Alpha-Red-Green- Blue 的格式。例如:

#RGB

#ARGB

#RRGGBB

#AARRGGBB

顏色資源位於value檔案夾下,建立一個.xml檔案,在裡面添加代碼

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <color name="hong">#ff0000</color>

    <color name="huang">#00ff00</color>

    <color name="lv">#0000ff</color>

</resources>


在資源檔中引用顏色資源:

<TextView android:id="@+id/text1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/str1"        android:textColor="@color/huang"/>

在布局檔案中添加如上代碼,引用格式為"@顏色資源檔名/color_name"(@color/huang)


在.java代碼中引用顏色資源,在.java中添加如下代碼

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    this.getWindow().setBackgroundDrawableResource(R.color.lv);    //設定背景顏色    //使用R.color.color_name(R.color,lv)方式引用    //.......    }}

                        字串資源


字串資源位於value檔案夾下,建立一個.xml檔案,在裡面添加代碼

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">HelloABC</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string><string name="str1">Hello Android!!</string></resources>


在資源檔中引用字串資源:

<TextView android:id="@+id/text1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/str1"    <!--str1為定義的字串資源-->        android:textColor="@color/huang"/>

在.java中引用字串資源:

使用函數

getString(R.string.string_name).toString();

eg:

String string = getString(R.string.str1).toString();


                                位元影像資源


drawable 資源是一些圖片或者顏色資源,主要用來繪製螢幕,通過Resources.get

Drawable() 方法獲得。 drawable 資源分為三類: Bitmap File (位元影像檔案)、 Color Drawable

(顏色)、 Nine-Patch Image  (九片圖片)。這裡只講述常用的位元影像檔案的使用。

Android 中支援的位元影像檔案有 png 、jpg 和 gif 。




將圖片複製到res/drawable檔案中,然後重新整理項目,項目自動更新,並獲得位元影像id,在R.java中drawable中可以看到。

Ps:圖片的名稱只能為a-z1-9.png,不能出現大些字母


在布局檔案中引用位元影像:

<ImageView android:id="@+id/pic1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/prenren"/>

如上代碼,在Android:src中引用圖片源,格式為"@drawable/picture_name"("@drawable/prenren")

在菜單檔案中引用位元影像作為表徵圖:

<item android:title="edit"

        android:icon="@drawable/prenren"

</item>  


在java代碼中可以通過以下方法引用:

ImageView myimageview = (ImageView)findViewById(R.id.imageview1);Resources r=getResources();//通過 Resources 獲得 Drawable 執行個體Drawable d=r.getDrawable(R.drawable.picture_name);//設定 ImageView 的 ImageDrawable 屬性顯示圖片maimageview.setImageDrawable(d);

也可以合起來寫:

this.getResources().getDrawable(R.drawable.prenren);




本文出自 “無用大叔” 部落格,請務必保留此出處http://aslonely.blog.51cto.com/6552465/1614844

Android 資源詳解(一) 顏色、字串、位元影像資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.