導入
之前都是斷斷續續的看了一些於如何使用android開發的文章、資料等,到目前位置很多基礎的東西都不清楚,於是去學習了別人的課程,才了認識了R類、findViewById方法查找組件、項目引入資源國際化、@string查找字符。
在學習之前我們需要安裝環境,這里我采用開發環境是:win7 x64+jdk8+adt-bundle-windows-x86_64-20131030.zip(adt-bundle是集成了adt、eclipse、andrlid sdk等,解壓運行eclipse.exe就可以直接使用)
jdk8+adt-bundle-windows-x86_64-20131030.zip下載鏈接: https://pan.baidu.com/s/1gfoyKN5 密碼: d8dt
adt-bundle ide運行效果如下:
認識R類
R類管理資源包含:
1)layout下中的andoid:id、android:text等資源信息等
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也一樣)。
<?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方法查找組件
為了實現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屬性值。
@string查找字符
在我們默認添加一個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: