【android-tips】关于string.xml的使用

 (转载请注明出处:http://blog.csdn.net/buptgshengod 

1.介绍

    
              在制作android应用的时候,常常用到很多string,就是一些文字,这里介绍一种方法,将所有string封装在一个string.xml文件中,通过styles.xml控制string的大小等属性。在代码中,只要调用string的id即可。这样符合面向对象的编程思想,而且也能使android应用更好的适应各种分辨率的手机!

2.具体实现

      首先,我们在value文件夹下建立string.xml用于存放string,建立styles.xml用于控制string的属性。(无视其他xml)

在string.xml中,我们声明一些string,注意name
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, GameActivity!</string>
    <string name="app_name">镖镖必达</string>
    <string name="help">帮助信息\n此游戏是一款单指飞镖游戏\n点击屏幕发射飞镖\n连击可以获得更高分数</string>
    <string name="about">关于游戏\n\n蓟门边IT创意工作室出品\nRhythmMaker原班人马打造\n单指RPG手游巅峰之作\n技术:zixiyaoren@gmail.com
        \n美术:Rou7in@gmail.com</string>
    <string name="title_activity_other">OtherActivity</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>

在styles.xml中
<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
<style name="label_white">
		<item name="android:textSize">38px</item>//字体大小
    	<item name="android:textColor">#0000FF</item>//颜色是蓝色
    	<item name="android:gravity">center</item>//居中
    	<item name="android:textStyle">bold</item>//加粗
	</style>
    
</resources>
配置完xml文件,我们看看在代码中怎么调用!

   比如我们要调用string.xml中命名为help的string。只要在对象中类似于这种 setText(R.string. help );调用方法就可以,跟图片的用法是一样的!



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值