-
- 今天的項目開始
- 項目正式開始之前,總得需要了解整個項目
- 一、先簡紹目錄:
- java:我們寫Java代碼的地方,業務功能都在這裏實現
- res:存放我們各種資源文件的地方,有圖片,字符串,動畫,音頻等,還有各種形式的XML文件
-
使用mipmap會在圖片縮放在提供一定的性能優化,分辨率不同系統會根據屏幕分辨率來選擇hdpi,mdpi,xmdpi,xxhdpi下的對應圖片,所以你解壓別人的apk可以看到上述目錄同一名稱的圖片,在四個文件夾下都有,只是大小和像素不一樣而已!
- 如果你想禁止Android不跟隨屏幕密度加載不同文件夾的資源,只需在AndroidManifest.xml文件中添加android:anyDensity="false"字段即可!
-
1.先說下圖片資源:
- drawable:存放各種位圖文件,(.png,.jpg,.9png,.gif等)除此之外可能是一些其他的drawable類型的XML文件
- mipmap-hdpi:高分辨率,一般我們把圖片丟這裏
- mipmap-mdpi:中等分辨率,很少,除非兼容的的手機很舊
- mipmap-xhdpi:超高分辨率,手機屏幕材質越來越好,以後估計會慢慢往這裏過渡
- mipmap-xxhdpi:超超高分辨率,這個在高端機上有所體現
2.接着說下佈局資源:
- layout:該目錄下存放的就是我們的佈局文件。
3.接下來說下菜單資源:
- menu:在以前有物理菜單按鈕,即menu鍵的手機上,用的較多,現在用的並不多
4.接下來說下values目錄:
- demens.xml:定義尺寸資源
- string.xml:定義字符串資源
- styles.xml:定義樣式資源
- colors.xml:定義顏色資源
- arrays.xml:定義數組資源
- attrs.xml:自定義控件時用的較多,自定義控件的屬性!
5.在接着說下這個raw目錄: 用於存放各種原生資源(音頻,視頻,一些XML文件等),我們可以通過openRawResource(int id)來獲得資源的二進制流!其 實和Assets差不多,不過這裏面的資源會在R文件那裏生成一個資源id而已
6.最後還有個動畫的,動畫有兩種:屬性動畫和補間動畫:
- animator:存放屬性動畫的XML文件
- anim:存放補間動畫的XML文件
二、使用這些資源
我們所有的資源文件都會在R.java文件下生成一個資源id,我們可以通過這個資源id來完成資源的訪問,使用情況有兩種:Java代碼中使用和XML代碼中使用。 -
Java代碼中使用:
Java 文字:
txtName.setText(getResources().getText(R.string.name));
圖片:
imgIcon.setBackgroundDrawableResource(R.drawable.icon);
顏色:
txtName.setTextColor(getResouces().getColor(R.color.red));
佈局:
setContentView(R.layout.main);
控件:
txtName = (TextView)findViewById(R.id.txt_name);
XML代碼中使用:
通過@xxx即可得到,比如這裏獲取文本和圖片:
<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background = "@drawable/img_back"/>
三、深入瞭解
剖析工程裏三個比較重要的文件: MainActivity.java,佈局文件:activity_main和Android配置文件:AndroidManifest.xml -
MainActivity.java:
代碼如下
package jay.com.example.firstapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
代碼分析:借圖說話
佈局文件:activity_main.xml:
代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
代碼分析:借圖說話
我們定義了一個LinearLayout線性佈局,在xml命名空間中定義我們所需要使用的架構,來自於①
AndroidManifest.xml配置文件:
代碼如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jay.com.example.firstapp" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
代碼分析:借圖說話
-
安卓項目正式啓動。
-
我在慕課網開始學習基礎
Android攻城獅的第一門課(入門篇)
- 五大布局,還有自己學習彈性佈局
-