Android之XML学习

来源:互联网 发布:netbeans php常用插件 编辑:程序博客网 时间:2024/06/09 21:38

一:基本语法

1、XML 声明
XML 声明文件的可选部分,如果存在需要放在文档的第一行,如下所示:
<?xml version="1.0" encoding="utf-8"?>
以上实例包含 XML 版本(
UTF-8 也是 HTML5, CSS, JavaScript, PHP, 和 SQL 的默认编码。

2、 XML 元素?
XML 元素指的是从(且包括)开始标签直到(且包括)结束标签的部分。
一个元素可以包含:
其他元素
文本
属性
或混合以上所有...



<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">SleepAwakeAutoTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="awake">IMEI NUM:</string>
    <string name="second">Second(s)</string>
    <string name="sleep">IMEI DISPLAY</string>
    <string name="start">write IMEI</string>
    <string name="stop">read IMEI</string>
    <string name="instruction">Instructions:</string>
    <string name="time_instruction"> Awake time is the time of the unsuspend state, Sleep time is of suspend</string>
    <string name="operate"> Press Start to start the auto test. Press the Cancel in the nofitcation to stop this test</string>
    <string name="sleepawake">SleepAwake</string>
    <string name="cancel">Cancel</string>
    <string name="action_settings">Settings</string>
    <string name="random">Random Mode</string>
    <string name="TestTimes">Test times:</string>
    <string name="sample_device_admin_description">Descrption of Administration</string>
    <string name="sample_device_admin">setAdministration</string>
    <string name="Enable">Enable</string>
    <string name="Disable">Disable</string>
</resources>


xml占位符:

写一个简单的案例。

在strings.xml中写入

[html] view plain copy
  1. <string name="string_test_1">学号:%1$d ;姓名:%2$s ;成绩:%3$.2f</string>  

在Activity.java中写入

[html] view plain copy
  1. String testStr = getResources().getString(R.string.string_test_1);  
  2. String result = String.format(testStr,1001,"张三",9.235);  
  3. System.out.println(result);  

打印结果为:

[html] view plain copy
  1. 学号:1001 ;姓名:张三 ;成绩:9.24  

原创粉丝点击