手把手教你做安豆计算器(五)-优化资源的使用

来源:互联网 发布:淘宝网谁开发的 编辑:程序博客网 时间:2024/06/10 22:36

第6节 优化资源使用

字符串、颜色以及各种尺寸相关的值都属于程序的资源。
我们前面都是直接使用的这些资源,例如,

<resources>    ......    <style name="SymbolDarkBtnStyle">        <item name="android:layout_margin">0.5dp</item>        <item name="android:textSize">35sp</item>        <item name="android:textColor">#FF000000</item>        <item name="android:background">@drawable/symbol_dark_button_selector</item>    </style></resources>

其实这些资源可以分门别类的放到单独的位置定义,然后通过@引用的方式进行使用。

6.1 优化颜色

颜色的定义和使用,

  1. 颜色应该定义到res\values\colors.xml当中,

    <resources>    <color name="colorBtnText">#FF000000</color></resources>
  2. 在需要使用它的地方采用引用的方式,@color/

    <style name="SymbolDarkBtnStyle">    <item name="android:textColor">@color/colorBtnText</item></style>

6.2 优化尺寸

尺寸的定义和使用,

  1. 尺寸应该定义到res\values\dimens.xml当中,

    <resources>    <dimen name="btnTextSize">35sp</dimen>    <dimen name="btnMargin">0.5sp</dimen></resources>
  2. 在需要使用它的地方采用引用的方式,@dimen/

    <style name="SymbolDarkBtnStyle">    <item name="android:layout_margin">@dimen/btnMargin</item>    <item name="android:textSize">@dimen/btnTextSize</item></style>

6.3 优化字符串

字符串的定义和使用,字符串应该定义到res\values\strings.xml当中,

<resources>    <string name="btn_add">+</string></resources>

6.3.1 xml使用字符串

在布局文件中使用字符串,应该采用引用的方式,@string/

<Button android:id="@+id/btn_add"         android:text="@string/btn_add"        android:layout_weight="1"/>

6.3.2 代码使用字符串

在代码中使用字符串,应该采用如下方式,

String str = context.getString(R.string.btn_add);

其中context是一个应用到上下文环境。对一个Activity来说,就是Activity自身MainActivity.this

将代码中所有使用资源的地方,都按照前面介绍的方式进行改造。以后在修改代码的时候,就不会牵一发而动全身了,只需要修改一处,就能够把所有需要修改的地方都修改到了。

6.3.3 多国语言支持

安卓系统,支持多国语言,现在我们将添加对中文支持。

  1. res目录下,点击右键,启动创建android resource的向导;

  2. File name栏,输入strings.xml,再选中下方的locale,添加到右边;

  3. 选中zh,代表中文;

  4. 在创建出来的中文的res\values-zh\strings.xml文件中,仿照res\values\strings.xml的内容,添加中文语言;

    <resources>    <string name="app_name">计算器</string></resources>

这里的app_name,在AndroidManifest.xml文件中被使用,里面使用了android:label属性,指定这个应用的名称。

<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity></application>

至此,中文语言的支持,添加完成。在设备上运行程序后,会看到现在“计算器”应用到名称已经变成中文的计算器了。

6.4 style优化

已经定义的Style中,有很多相同的部分,可以将它们提取出来共同使用。
style可以继承自另一个style,

  1. 将共同使用的部分定义到BaseBtnStyle当中,

    <style name="BaseBtnStyle">    <item name="android:layout_margin">@dimen/btnMargin</item>    <item name="android:textSize">@dimen/btnTextSize</item>    <item name="android:textColor">@color/colorBtnText</item></style>
  2. 各种风格的Button继承BaseBtnStyle,使用parent=关键字;然后再加入不同的元素,

    <style name="DigitalBtnStyle" parent="@style/BaseBtnStyle">    <item name="android:background">@drawable/digital_btn_selector</item></style><style name="SymbolDarkBtnStyle" parent="@style/BaseBtnStyle">    <item name="android:background">@drawable/symbol_dark_btn_selector</item></style><style name="SymbolLightBtnStyle" parent="@style/BaseBtnStyle">    <item name="android:background">@drawable/symbol_light_btn_selector</item></style>

现在资源使用方式的优化就完成了。


/*******************************************************************/
* 版权声明
* 本教程只在CSDN和安豆网发布,其他网站出现本教程均属侵权。

*另外,我们还推出了Arduino智能硬件相关的教程,您可以在我们的网店跟我学Arduino编程中购买相关硬件。同时也感谢大家对我们这些码农的支持。

*最后再次感谢各位读者对安豆的支持,谢谢:)
/*******************************************************************/

0 0