首頁  >  文章  >  後端開發  >  詳解Android XML檔案使用的範例程式碼

詳解Android XML檔案使用的範例程式碼

黄舟
黄舟原創
2017-03-09 16:50:051836瀏覽

詳解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="false"android: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配置文件,在我看来就是为了达到显示层和数据层的分离,提高了可维护性,也是我们的程序代码变得简洁。


以上是詳解Android XML檔案使用的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn