详解Android XML文件使用的示例代码

详解Android XML文件使用的示例代码

一、布局文件:在layout目录下,使用比较广泛;

  我们可以为应用定义两套或多套布局,例如:可以新建目录layout_land(代表手机横屏布局),layout_port(代表手机竖屏布局),系统会根据不同情况自动找到最合适的布局文件,但是在同一界面的两套不同布局文件文件名应该是相同的,只是放在了两个不同的目录下。

二、图片文件:在drawable目录下,从2.1版本以后分为三个目录,

drawable-hdpi里面存放高分辨率的图片,如WVGA (480×800),FWVGA (480×854)

drawable-mdpi里面存放中等分辨率的图片,如HVGA (320×480)

drawable-ldpi里面存放低分辨率的图片,如QVGA (240×320)

系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片。  

在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片

我们可以将已经做好的图片放到该目录下,或者通过自定义XML文件来实现想要的图片,例如我们可以定义shapge_1.xml放到drawable目录下,内容如下:

<shape xmlns:android=http://schemas.android.com/apk/res/android   android:shape=oval> 
<!--android:shape=oval表示所要绘制的图形是一个椭圆,认是rectangle,长方形-->
<gradient 
    android:startColor=#0055ff88 
    android:centerColor=#0055ff00 
    android:centerY=0.75 
    android:endColor=#00320077 
    android:angle=270 
/> 
<!--gradient 产生颜色渐变 android:angle 从哪个角度开始变 只有90的整数倍可以 -->
<solid android:color=#ff4100ff/> 
<!--solid表示图形是实心的,填充里面,#ff4100ff为填充颜色-->
<stroke 
    android:width=2dp
    android:color=#ee31ff5e 
    android:dashWidth=3dp 
    android:dashGap=2dp /> 
<!-- 描边 采用那样的方式将外形轮廓线画出来,width表示笔的粗细,dashWidth表示小横线的宽度,dashGap表示小横线之间的距离-->
<padding
    android:left=7dp
    android:top=7dp
    android:right=7dp 
    android:bottom=7dp /> 
<!--和CSS中的padding应该是一个道理-->
<corners android:radius=6dp /> 
<!--corners表示是有半径为5像素的圆角-->
</shape>

当我们想让一个控件根据不同状态显示不同图片,可以直接在程序中控制,也可以在drawable目录建立XML文件达到相同的效果,例如:我们可以在drawable目录下新建文件button_back.xml

<?xml version=1.0 encoding=UTF-8?>
<selector xmlns:android=http://schemas.android.com/apk/res/android>
    <item  android:state_pressed=falseandroid:drawable=@drawable/xxx1 />
    <item  android:state_pressed=true  android:drawable=@drawable/xxx2 />
    <item  android:state_focused=true  android:drawable=@drawable/xxx3 />
    <-- 这里还可以加N多效果和动作 只要你用的到 -->
    <item  android:drawable=@drawable/xxx4 />
</selector>

以上XML文件可以实现一个控件(假设为button),获取焦点,按下按钮,正常状态下显示不同图片效果,只需要在定义控件是引用该文件名即可,例如:

<Button
     android:id=@+id/Button
     android:layout_width=wrap_content
     android:layout_height=wrap_content
     android:background=@drawable/button_add_x>
</Button>
<!--android:background=@drawable/button_back指向button_back.xml文件-->

但是当我们的条件不是系统已有的事件类型,例如根据ImageView根据一个变量var的值的不同显示不同的图片,应该怎么办呢?可以在程序中写如下代码

if (条件1)
{ 
image.setBackground(R.id.xxx1); 
}
else if (条件2)
{ 
image.setBackground(R.id.xxx2); 
} ...

或者可以用另一个简便的方法实现相同的功能,在res/drawable下建立一个xml文件内容如下

<level-list xmlns:android=http://schemas.android.com/apk/res/android>
    <item android:maxLevel=9 android:drawable=@drawable/battery_0 />
    <item android:maxLevel=39 android:drawable=@drawable/battery_1 />
    <item android:maxLevel=69 android:drawable=@drawable/battery_2 />
    <item android:maxLevel=89 android:drawable=@drawable/battery_3 />
    <item android:maxLevel=100 android:drawable=@drawable/battery_4 />
</level-list>

然后在layout中把imageview的src设置成已创建好的xml文件 ,程序中变换图片时,只需要使用 imageview.getDrawable().setLevel(50);
Android会根据level的值自动选择对应的图片。手机显示剩余电量就是用这个方法显示不同图片的。

三、菜单文件:在menu目录下,写代码时只需在onCreateOptionsMenu方法中用MenuInflater装载进去就OK了。格式如下,

<menu xmlns:android=http://schemas.android.com/apk/res/android>
    <item android:id=@+id/enabled_item
        android:title=Enabled
        android:icon=@drawable/stat_happy />

    <item android:id=@+id/disabled_item
        android:title=disabled
        android:enabled=false
        android:icon=@drawable/stat_sad />

    <item android:id=@+id/enabled_item_2
        android:title=Enabled
        android:icon=@drawable/stat_happy />

    <item android:id=@+id/disabled_item_2
        android:title=disabled
        android:enabled=false
        android:icon=@drawable/stat_sad />
</menu>

四、resource文件,在values目录下,之所以称之为resource文件,是因为values目录下xml文件都是以resource作为根节点,

  1.strings.xml  定义字符串的文件,格式如下: 

<resources>
    <string name=hello>Hello World!</string>
    <string name=app_name>我的应用程序</string>
</resources>

  2.colors.xml  定义颜色的文件,格式如下:

<resources>
    <!--定义图片颜色-->
    <drawable name=screen_background_black>#ff000000</drawable>
    <drawable name=translucent_background>#e0000000</drawable>
    <drawable name=transparent_background>#00000000</drawable>
    <!--定义文字颜色-->
    <color name=solid_red>#f00</color>
    <color name=solid_blue>#0000ff</color>
    <color name=solid_green>#f0f0</color>
    <color name=solid_yellow>#ffffff00</color>
</resources>

  3.arrays.xml  定义数组的文件,格式如下:

<resources>
    <string-array name=planets>
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
        <item>Pluto</item>
    </string-array>

    <integer-array name=numbers>
        <item>100</item>
        <item>500</item>
        <item>800</item> 
    </integer-array>
</resources>

4.styles.xml  定义样式的文件,分为两种用途:

   Style:以一个单位的方式用在布局XML单个元素(控件)当中。 例如:我们可以为TextView定义一种样式风格,包含文本的字号大小和颜色,然后将其用在TextView特定的实例。
   Theme:以一个单位的方式用在应用中所有的Activity当中或者应用中的某个 Activity当中。 比如,我们可以定义一个Theme,它为window frame和panel 的前景和背景定义了一组颜色,并        为菜单定义可文字的大小和颜色属性,可以将这个Theme应用在你程序当中所有的Activity里。

<resources>
   <!--Theme,可以用来定义activity的主题-->
   <style name=Theme.Transparent>
        <item name=android:windowIsTranslucent>true</item>
        <item name=android:windowAnimationStyle>@android:style/Animation.Translucent</item>
        <item name=android:windowBackground>@drawable/transparent_background</item>
        <item name=android:windowNoTitle>true</item>
        <item name=android:colorForeground>#fff</item>
    </style>
    <!--Style,可以用来定义某个View元素,这里是ImageView的样式-->
    <style name=ImageView120dpi>
        <item name=android:src>@drawable/stylogo120dpi</item>
        <item name=android:layout_width>wrap_content</item>
        <item name=android:layout_height>wrap_content</item>
    </style>
</resources>

个人认为,其实不管是Theme还是Style,其实只是应用的范围不同而已,区分的话应该是根据android:name=xxxx的xxxx来区分吧,很明显是不同的。

  5.dimen.xml  定义单位的文件,android中度量单位有以下几种:

    px(象素): 屏幕实际的象素,常说的分辨率1024*768pixels,就是横向1024px, 纵向768px,不同设备显示效果相同。

    in(英寸): 屏幕的物理尺寸, 每英寸等于2.54厘米。

    mm(毫米): 屏幕的物理尺寸。

    pt(点) : 屏幕的物理尺寸。1/72英寸。

    dp/dip : 与密度无关的象素,一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px。但dp和px的比例会随着屏幕密度的变化而改变,不同设备有不同的显示效果

    sp : 与刻度无关的象素,主要用于字体显示best for textsize,作为和文字相关大小单位。

<resources> 
    <dimen name=one_pixel>1px</dimen> 
    <dimen name=double_density>2dp</dimen> 
    <dimen name=sixteen_sp>16sp</dimen> 
</resources>

 6.attrs.xml  定义属性文件,主要用在自定义的组件中,具体使用方法会在后续的如何使用自定义组件中详细介绍,其格式如下:

<resources>     
    <declare-styleable name=MyView>     
        <attr name=textColor format=color />     
        <attr name=textSize format=dimension />     
    </declare-styleable>     
</resources>

五、动画文件  在anim目录下,动画资源分为两种,

1.实现图片的translate、scale、rotate、alpha四种变化,还可以设置动画的播放特性,称为Tween动画。

<set xmlns:android=http://schemas.android.com/apk/res/android>               
    <translate android:interpolator=@android:anim/accelerate_interpolator
         android:fromXDelta=0 android:toXDelta=200 android:fromYDelta=0
         android:toYDelta=180 android:duration=2000 />
    <scale android:interpolator=@android:anim/accelerate_interpolator
         android:fromXScale=1.0 android:toXScale=2.0 android:fromYScale=1.0
         android:toYScale=2.0 android:pivotX=150% android:pivotY=150%
         android:duration=2000 />
    <alpha android:fromAlpha=1.0 android:toAlpha=1.0
         android:duration=@android:integer/config_mediumAnimTime />
    <rotate ....各个属性></rotate>
    <Interpolator >可以使用其子类和属性定义动画的运行方式,先快后慢,先慢后快等</Interpolator>
</set>

2.帧动画,逐帧播放设置的资源,称为Frame动画。

<animation-list xmlns:android=”http://schemas.android.com/apk/res/android” 
android:oneshot=”true”> 
    <item android:drawable=”@drawable/rocket_thrust1″ android:duration=”200″ /> 
    <item android:drawable=”@drawable/rocket_thrust2″ android:duration=”200″ /> 
    <item android:drawable=”@drawable/rocket_thrust3″ android:duration=”200″ /> 
</animation-list>

六、raw目录下的文件,是直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。一般为应用要用到的音频或视频文件等等

  要使用这些资源,可以调用Resources.openRawResource(),参数是资源的ID,即R.raw.somefilename。

七、xml目录下的文件,是程序中需要使用的普通xml文件。在运行时可以通过调用Resources.getXML()读取。

八、assets目录下的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件

  1. 先在Activity里面调用getAssets()来获取AssetManager引用。

  2. 再用AssetManager的open(String fileName, int accessMode)方法则指定读取的文件以及访问模式就能得到输入流InputStream。

  3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close()。

  4.调用AssetManager.close()关闭AssetManager。        


总结:其实android中定义如此多的XML配置文件,在我看来就是为了达到显示层和数据层的分离,提高了可维护性,也是我们的程序代码变得简洁。

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念