極力推薦文章:歡迎收藏
Android 乾貨分享 android
本篇文章主要介紹 Android
開發中的部分知識點,經過閱讀本篇文章,您將收穫如下內容:程序員
- 多語言 String 資源
- 多屏幕 Image 資源
- 橫豎屏 Layout 佈局
- 不一樣版本SDK
- Array 數組資源
- Color 顏色資源
- Dimen 尺寸資源
- style樣式 資源
- assert 文件夾下的原始資源
- raw 文件夾下的資源
- anim 文件夾下的資源
Android
中常常會使用資源文件來填充View
或者 實現app
相關的功能,本篇文章總結了Android
中常見的一些資源的使用方法。數組
String
主要用於存放系統字符串資源,字符串資源跟其餘資源相似,也是在values
文件夾下。Android
字符串資源支持多語言,使用方法以下:微信
valuess-(ISO語言代碼)
app
Java使用方法以下:R.string.<string_name>
引用字符串資源ide
Java中 字符串資源獲取方法:佈局
tv_res = (TextView) findViewById(R.id.tv_res); String mString=getResources().getString(R.string.hello_world); tv_res.setText(mString);
使用方法以下:@string/<string_name>
引用字符串資源學習
XML
中 字符串資源獲取方法:測試
<TextView android:id="@+id/tv_res" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="測試 res 資源文件使用方法" />
存放路徑以下:valuess-(ISO語言代碼)/strings.xml
動畫
存儲多語言字符串資源文件:
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> ... ... <string name="hello_world">Hello world!</string> ... ... </resources>
Image
主要用於存放系統圖片資源,圖片資源跟其餘資源相似,也是在res
文件夾下。
經常使用存放圖片資源的文件夾以下:
xml
中使用圖片資源:
<ImageView android:id="@+id/img_res_usb" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" />
Java中使用圖片資源 :
ImageView mImageView=(ImageView) findViewById(R.id.img_res_usb); mImageView.setImageResource(R.drawable.ic_launcher);
由Array
主要用於存放系統佈局資源,佈局資源跟其餘資源相似,也是在res
文件夾下。
於Andoid
設備屏幕大小不統一,所以Android
爲適配多屏幕實現多佈局。
固定Activity的顯示方向:
<!-- 豎屏 portrait --> <activity android:name=".Activity.ActivityMethods" android:screenOrientation="portrait" /> <!-- 橫屏 landscape --> <activity android:name="com.programandroid.TextView.TextViewMethod" android:screenOrientation="landscape" />
android SDK版本支持:
<uses-sdk android:minSdkVersion="24" android:targetSdkVersion="27" />
SDK 版本判斷:
//判斷當前手機設備SDK 版本是不是在Android M 6.0 之上 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){ }else { }
Array
主要用於存放系統數組資源,數組資源跟其餘資源相似,也是在values
文件夾下。
xml 中聲明數組資源以下:
<string-array name="fav_phone"> <item>Iphone</item> <item>華爲</item> <item>小米</item> <item>oppo</item> <item>vivo</item> <item>錘子</item> </string-array>
在XML 中直接使用數組資源:
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/fav_phone" />
Java 中使用字符串資源:
String[] mArray=getResources().getStringArray(R.array.fav_phone);
Color
主要用於存放系統顏色資源,顏色資源跟其餘資源相似,也是在values
文件夾下。
Colors.xml 中的顏色值
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="grey">#A9A9A9</color> <color name="black">#000000</color> <color name="white_line">#d8d8d8</color> </resources>
xml 中使用Color 資源方法:
<Button android:id="@+id/btn_res_color" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="ResColorMethod" android:text="顏色資源設置" android:textColor="@color/black" />
Java中顏色資源使用方法:
Button mButton=(Button) findViewById(R.id.btn_res_color); mButton.setTextColor(getResources().getColor(R.color.black));
使用系統Color 類中的資源:
Button mButton=(Button) findViewById(R.id.btn_res_color); // mButton.setTextColor(getResources().getColor(R.color.black)); mButton.setTextColor(Color.RED);
Dimen
主要用於規範化Android
尺寸,邊距等資源。尺寸資源跟其餘資源相似,也是在values
文件夾下。
xml 中使用Dimen 資源:
<Button android:id="@+id/btn_res_dimen" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="ResDimenMethod" android:textSize="@dimen/abc_action_bar_default_height" android:paddingLeft="@dimen/abc_action_bar_default_height" android:text="尺寸資源設置" />
Java代碼中使用Dimen資源:
Button mButton=(Button) findViewById(R.id.btn_res_color); // mButton.setTextColor(getResources().getColor(R.color.black)); mButton.setTextColor(Color.RED); mButton.setTextSize(getResources().getDimension(R.dimen.activity_horizontal_margin));
style
主要是統一規範app
系統主題樣式等資源。
style樣式資源控制舉例:
<resources> <style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"></style> <!-- 自定義loading dialog樣式 --> <style name="loading_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/loading_bg</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
此文件夾爲原始資源文件夾,文件下的內容不會被編譯,此目錄同src
及res
同級。
獲取asset下文件字符串、位圖:
public void ResAssetMethod(View view) { String fileString = ReadStrFromFile("test.txt"); Toast.makeText(getApplicationContext(), "文件內容" + fileString, Toast.LENGTH_LONG).show(); Bitmap btnBitmap = ReadImageFromAssetFile("img/ic_launcher.png"); ImageView img = (ImageView) findViewById(R.id.img_res_assert); img.setImageBitmap(btnBitmap); }
獲取asset下文件字符串 方法:
/** * @param string */ private String ReadStrFromFile(String filename) { if (TextUtils.isEmpty(filename)) { Toast.makeText(getApplicationContext(), "文件不能爲空", Toast.LENGTH_SHORT).show(); return null; } String assetString = null; try { InputStream inputStream = getAssets().open(filename); byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes); assetString = new String(bytes, "utf-8"); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } return assetString; }
獲取asset下 圖片方法:
/** * @param string */ private Bitmap ReadImageFromAssetFile(String filename) { if (filename == null) { return null; } Bitmap bitmap = null; try { InputStream inputStream = getAssets().open(filename); bitmap = BitmapFactory.decodeStream(inputStream); } catch (IOException e) { e.printStackTrace(); } return bitmap; }
raw
主要用於存放Android
資源。
raw 資源文件夾引用方法:
private MediaPlayer mMediaPlayer; private boolean isplaying = false; public void ResRAWMethod(View view) { mMediaPlayer = MediaPlayer.create(ResourceActivity.this, R.raw.bootaudio); if (!isplaying) { mMediaPlayer.start();// 開始播放 isplaying = true; Toast.makeText(getApplicationContext(), "正在播放中", Toast.LENGTH_LONG) .show(); } mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { if (mMediaPlayer != null) { try { isplaying = false; mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.release(); mMediaPlayer = null; } catch (Exception e) { } } } }); }
Anim
主要用於存放Android
動畫資源。
anim 配置:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/bird0001_risk" android:duration="80"/> <item android:drawable="@drawable/bird0002_risk" android:duration="80"/> <item android:drawable="@drawable/bird0003_risk" android:duration="80"/> <item android:drawable="@drawable/bird0004_risk" android:duration="80"/>
anim 的使用:
<ImageView android:id="@+id/img" android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="center_horizontal" android:background="@anim/frame_animation" />
至此,本篇已結束,若有不對的地方,歡迎您的建議與指正。同時期待您的關注,感謝您的閱讀,謝謝!