這幾天要總結一下android開發中的用戶資源訪問。java
android中的用戶資源存在項目工程中res文件夾下,有字符串、顏色、大小、數組、佈局、樣式、主題等資源,這些資源能夠在xml文件中引用,也能夠在android源碼文件中使用,今天總結一下字符串、顏色、大小、數組、佈局和圖片資源。android
總的來講,在xml文件中引用的格式爲[<package>.]@/XXX/name;在源碼中引用格式是[<package>.]R.XXX.name。數組
先貼上在xml文件中引用的代碼(在佈局xml文件中)和在源碼中引用代碼。app
//main.xml佈局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="main.testresources.MainActivity" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/lor" android:textSize="@dimen/dim" android:text="@string/tv1" android:background="@drawable/ic_launcher" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn1" android:textColor="@color/lor2" android:textSize="@dimen/dim2" android:background="@drawable/ic_launcher"/> <!-- 在ADT 16.0以上,在一些沒有文本顯示的控件裏,須要加上功能描述,即 android:contentDescription="@string/jieshao";android:src="@drawable/test"是設置圖片顯示 --> <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/jieshao" android:src="@drawable/test"/> <!-- 引用外部佈局文件,這樣會將外部佈局文件添加到該佈局文件中,成爲該佈局的一部分,佈局文件名稱layout_test.xml--> <include layout="@layout/layout_test" /> </LinearLayout>
//layout_test.xml佈局文件,供main.xml文件調用 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Button2" /> </LinearLayout>
//android源碼文件 public class MainActivity extends ActionBarActivity implements OnClickListener{ protected TextView tv1=null; protected Button btn1=null; protected int num=0; protected StringBuffer stringBuffer=null; protected String[] arr=new String[3]; protected String str=null; protected int col=0; protected float dim=0; protected ImageView iv=null; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//引用佈局文件,並加載顯示 tv1=(TextView)findViewById(R.id.tv1); btn1=(Button)findViewById(R.id.btn1); iv=(ImageView)findViewById(R.id.iv1); str=getResources().getString(R.string.tv1);//取得字符串資源 tv1.setText(str); col=getResources().getColor(R.color.lor);//取得顏色資源 btn1.setTextColor(col);//設置文字顯示顏色 btn1.setBackgroundColor(col);//設置按鈕背景顏色 dim=getResources().getDimension(R.dimen.dim);//取得大小資源 btn1.setTextSize(dim);//設置顯示文字大小 arr=getResources().getStringArray(R.array.world);//取得數組資源 btn1.setText(arr[0]);//取數組中的第一個元素做爲按鈕顯示文本 tv1.setBackground(getResources().getDrawable(R.drawable.ic_launcher));//取得圖片資源,並設置成背景圖片 iv.setImageResource(R.drawable.test);//設置顯示圖片 btn1.setOnClickListener(MainActivity.this); }
(1)、字符串資源ide
存在位置:/res/values/strings.xml,根元素是<resources></resources>,用<string></string>標記,以下代碼:佈局
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestResources</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="tv1">第一個</string> <string name="btn1">點我切換顯示</string> </resources>
(2)、顏色資源ui
存在位置:/res/values/colors.xml,根元素爲<resources></resources>,用<color></color>標記,以下代碼this
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 半透明,紅色 --> <color name="lor">#66ff0000</color> <!-- 半透明,綠色 --> <color name="lor2">#6600ff00</color> </resources>
(3)、大小資源code
存在位置:/res/values/dimens.xml,根元素爲<resources></resources>,用<dimen></dimen>標記,以下代碼xml
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="dim">30dp</dimen> <dimen name="dim2">40dp</dimen> </resources>
(4)、數組資源
存在位置:/res/values/arrays.xml,根元素爲<resources></resources>,用<integer-array></integer-array>或<string-array></string-array>標記,以下代碼
<?xml version="1.0" encoding="utf-8"?> <resources> <integer-array name="hello"> <item >1</item> <item >2</item> <item >3</item> </integer-array> <string-array name="world"> <item >one</item> <item >two</item> <item >three</item> </string-array> </resources>
(5)、佈局文件資源
存在位置:/res/layout/xxx.xml,根元素不定,通常是<LinearLayout></LinearLayout>,用各個組件節點標記,見上述貼出的佈局xml文件
(6)、圖片資源
存在位置:/res/drawable-xxxx/圖片名,非xml文件