极力推荐文章:欢迎收藏
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" />
至此,本篇已结束,若有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!