標籤: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 資源詳解(一) 顏色、字串、位元影像資源