1 使用顏色資源檔先定義colors.xml顏色資源檔,代碼如下<?xmlversion="1.0"encoding="utf-8"?><resources> <colorname="red_bg">#f00</color> <colorname="blue_text">#0000ff</color></resources>然後在activity_main.xml引用顏色資源,代碼如下 <TextView android:textColor="@color/blue_text" //設定視圖字型顏色 android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />然後在MainActivity類上設定視圖背景顏色protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setBackgroundDrawableResource(R.color.red_bg); //設定視圖背景顏色 }2 使用字串(string)常量字串引用格式為:"@string/字串資源名稱"3 使用尺寸(dimen)資源px 像素 螢幕上的真實像素表示in 尺寸 基於螢幕的物理尺寸dp 和密度無關的精度 相對螢幕物理密度的抽象單位sp 和精度無關的像素 和dp相似獲得資源:Resources r = getResources();dimens.xml代碼<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimenname="activity_horizontal_margin">16dp</dimen> <dimenname="activity_vertical_margin">16dp</dimen> <dimenname="text_width">150px</dimen>//文本寬度 <dimenname="text_height">100px</dimen>//文本高度 <dimenname="btn_width">30mm</dimen> //按鈕寬度 <dimenname="btn_height">10mm</dimen> //按鈕高度</resources>使用dimen資源//通過findViedById獲得Button執行個體 myButton = (Button)findViewById(R.id.testDimenButton); //獲得Resource執行個體 Resources r = getResources(); //通過getDimension方法獲得尺寸值 float btn_h = r.getDimension(R.dimen.btn_height); float btn_w = r.getDimension(R.dimen.btn_width); //設定按鈕的寬高 myButton.setWidth((int)btn_w); myButton.setHeight((int)btn_h);4 使用布局(layout)資源Android可以將螢幕中組件的布局方式定義在一個XML檔案中,可以調用Activity.setContentView()方法將布局檔案展示在Activity上。Android通過LayoutInflater類將XML檔案中的組件解析為可視化的視圖組件layout.xml代碼<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <!-- 以上四個屬性分別為命名空間,布局的寬,高(布滿視圖),組件布局(垂直) --> <!-- 以下嵌套一個TableLayout --> <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <TextView android:id="@+id/layoutTextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/test_view01" android:background="@color/bk_col"/> <!-- 以上屬性:引用組件ID,常值內容,該組件的寬,該組件的高,常值內容,背景顏色 --> <EditText android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=""/> </TableRow> <TableRowandroid:gravity="right"> <Button android:id="@+id/layoutButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_string"/> </TableRow> </TableLayout></LinearLayout>使用布局資源測試public class MainActivity extends Activity {
//定義組件
private Button myButton;
private EditText myEditText;
private TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設定Activity的介面布局
setContentView(R.layout.layout);
//展示組件
myTextView = (TextView)findViewById(R.id.layoutTextView01);
myButton = (Button)findViewById(R.id.layoutButton01);
myEditText = (EditText)findViewById(R.id.EditText01);
}5 使用菜單(menu)資源Android菜單分為選項菜單,操作功能表和子功能表結構組成: <menu>根項目,在<menu>根項目裡面會嵌套<item>和<group>子項目,<item>元素中也可嵌套<menu>形成子功能表<group>表示一個菜單組,屬性說明:id:唯一標示該菜單組的引用idmenuCategory:對菜單進行分類,定義菜單的有限級,有效值為container,system,secondary,alternativeorderInCategory:一個分類排序整數checkableBehavior:選擇行為,單選,多選還是其他,有效值為none,all,singlevisible:是否可見,true或者falseenabled:是否可用,true或者false<item>表示功能表項目,包含在<menu>或<group>中的有效屬性,<item>元素的屬性說明:id:唯一標示菜單的ID引用menuCategory:菜單分類orderInCategory:分類排序title:功能表標題字串titleCondensed:濃縮標題,適合標題太長的時候使用icon:菜單的表徵圖alphabticShortcut:字元快速鍵numericShortcut:數字快速鍵checkable:是否可選checked:是否已經被選visible:是否可見enabled:是否可用