正确使用android下的资源Res

引言

正确将文件资源放置在res文件下,可以减少不必要的麻烦,使用其中的软编码的格式,可以极大的降低由于硬编码导致的维护的不便。

1. String

直接在string.xml文件中定义好其中的变量就可以直接使用。

67221d78213bb

2. StringArray

有时候,我们需要传入一个数组时候,使用硬编码的格式的话,必然会导致需要更新时带来的代码重构问题,同样的,我们也可以在string.xml中定义好数组的定义,例如:

1
2
3
4
5
6
7
8
9
<string-array name="myStrArray">
<item>"恭喜发财"</item>
<item>"牛气冲天"</item>
<item>"心想事成"</item>
<item>"万事如意"</item>
<item>"开心一整年"</item>
<item>"烦恼消失掉"</item>
<item>"快乐常围绕"</item>
</string-array>

这样定义的时候,当我们需要使用到这个变量的时候,可以如下使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private String[] contents;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取contents的值
contents =getResources().getStringArray(R.array.myStrArray);
// 日志输出contents的值
for (int i = 0; i <contents.length ; i++) {
Log.d("TAG", "----------------: "+contents[i]);
}
// 将contents写入到文本中
textView = (TextView) findViewById(R.id.testx);
StringBuilder sb = new StringBuilder();
for (String content : contents) {
sb.append(content).append("\n");
}
textView.setText(sb.toString());
}

这样就可以将对应的array输出到日志或者文本框中:

1729258373889.png

3. Color

这里的使用也很简单,和string一样的使用方式,只是存放的位置不同:

4. drawable

这是一个背景的自定义器,例如,你可以开发一个中间是绿色,周围一圈是红色的背景色提供给按钮使用,在按钮被按下时,提醒用户单击了这给按钮。

在res文件下右击,选择新建resource file

67120b874b441

67132ec802893

创建完成后,我们由于需要一下长方形的背景,所以我们设置他的形状为矩形。

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>

</shape>
属性值 形状
ring 圆环
oval 椭圆
rectangle 矩形
line 线性

基本属性

属性值 描述
size 大小
solid 填充
corners 圆角
stroke 描边
gradient 水平or垂直
padding 边距

例如:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="@color/green" />
<stroke android:color="@color/red" android:width="10dp"/>
<corners android:radius="30dp"/>
</shape>

将textview的背景设置为:

1
android:background="@drawable/bg_green"

这样就能将颜色自定义到需要的组件上了。

1729310407189.png

同样的,当我们需要定义一个按钮点击时间的样式时,也可以使用这样的方式在外部定义和内部使用

这次我们需要创建的是选择器样式所以我们在创建时选择的Root element为selector

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置被点击时-->
<item android:state_pressed="true" android:drawable="@color/green"/>
<!-- 设置没有被点击时-->
<item android:state_pressed="false" android:drawable="@color/red"/>
<!--设置常规样式-->
<item android:drawable="@color/red"></item>
</selector>

同样直接在layout中修改样式的背景即可

1
2
3
4
5
6
7
<Button
android:id="@+id/buttonPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_bg"
android:text="按钮" />

5. Style和Theme

Style针对的是一个小的组件,而Theme可以使用在一整给layout中。

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 继承父样式,修改其中的部分样式-->
<style name="MyBtnStyle" parent="Theme.AppCompat.Light">
<item name="android:layout_height">50dp</item>
<item name="android:textSize">25sp</item>
<item name="android:textColor">@color/white</item>
</style>
</resources>

直接在所需要的组件中添加属性

1
style="@style/MyBtnStyle"