以前都是斷斷續續的看了一些於如何使用android開發的文章、資料等,到目前位置不少基礎的東西都不清楚,因而去學習了別人的課程,才了認識了R類、findViewById方法查找組件、項目引入資源國際化、@string查找字符。javascript
在學習以前咱們須要安裝環境,這裏我採用開發環境是:win7 x64+jdk8+adt-bundle-windows-x86_64-20131030.zip(adt-bundle是集成了adt、eclipse、andrlid sdk等,解壓運行eclipse.exe就能夠直接使用)java
jdk8+adt-bundle-windows-x86_64-20131030.zip下載連接: https://pan.baidu.com/s/1gfoyKN5 密碼: d8dtandroid
adt-bundle ide運行效果以下:windows
R類管理資源包含:app
1)layout下中的andoid:id、android:text等資源信息等eclipse
public static final class id { public static final int action_settings=0x7f080002; public static final int button1=0x7f080001; public static final int textView1=0x7f080000; }
public static final class string { public static final int action_settings=0x7f050001; public static final int app_name=0x7f050000; public static final int hello_world=0x7f050002; public static final int main_activity_button_title=0x7f050005; public static final int main_activity_msg=0x7f050003; public static final int main_activity_textView_text=0x7f050004; }
2)string對應的字段是res/values/strings.xml中的配置項信息(自動生成的,不須要認爲的修改R類,包含id也同樣)。ide
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HelloWord</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="main_activity_msg">Welcome hello word!</string> <string name="main_activity_textView_text">text view text,hello word!</string> <string name="main_activity_button_title">Button</string> </resources>
3)@color查找顏色函數
color對應的字段是res/values/colors.xml中的配置項信息(自動生成的,不須要認爲的修改R類,包含id也同樣)。佈局
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="yellow">#FFFF00</string> <color name="red">#FF0000</string> <color name="black">#000000</string> </resources>
使用時:學習
<Button ... android:background="@color/red"/>
4)@drawable查找圖片
只須要把png/jpeg/gif文件拷貝到新建的/res/drawable目錄下,或者拷貝到工程新建的默認drawable-xx目錄下
使用時:
<xx android:src="@drawable/img1" />
注意:官網推薦使用png,不推薦使用gif圖片。
drawable-xx文件夾的區別:
5)@dimen某個組件尺寸定義
須要在res/values/目錄下新建一個dimen.xml文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <dimen name="btn_height">24dp</dimen> 4 <dimen name="btn_weight">56dp</dimen> 5 </resources>
尺寸單位:
爲了實現findViewById查找組件,因而在新建的android項目的MainActivity佈局中拖拉如一個buttong(id=button1)
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginLeft="47dp" android:layout_marginTop="72dp" android:onClick="test" android:text="Button" />
button1添加android:onClick事件,並在MainActivity.java中添加監聽函數test
public void test(View view) { Toast.makeText(MainActivity.this, R.string.main_activity_msg, Toast.LENGTH_SHORT).show(); TextView textView = (TextView) this.findViewById(R.id.textView1); textView.setText(R.string.main_activity_textView_text); }
備註:
1)其中test事件函數的參數:響應的是哪一個控件則參數view就是哪一個控件對象;
2)Toast.makText是彈出提示框用的,想javascript中的alter函數;
3)findViewById這裏是經過R.id.textView1來搜索textView1,找到後修改了textView1的text屬性值。
在咱們默認添加一個button等按鈕時,其text值默認是Button值。這樣是android不推薦的使用方式,不利於國際化配置,擴展性很差。
上邊咱們的textView.text修改時採用了R.string.man_activity_textView_text的找到了新的text,該text使其是配置在res/values/strings.xml配置項中,其實咱們這裏的button也是支持從strings.xml配置項讀取方式:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginLeft="47dp" android:layout_marginTop="72dp" android:onClick="test" android:text="@string/main_activity_button_title" />
步驟一:在res/下新建中文資源國際化文件夾values-zh-rCN;
步驟二:把res/values下的strings.xml拷貝到res/values-zh-rCN下,並修改res/values-zh-rCN/strings.xml內容:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HelloWord</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="main_activity_msg">歡迎進入!</string> <string name="main_activity_textView_text">textView的內容!</string> <string name="main_activity_button_title">按鈕</string> </resources>
從新發布項目,並修改測試機的語言設置爲中文,以後查看該app:
修改測試機的語言設置爲英文,以後查看該app: