這篇文章主要介紹了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)
系統會根據機器的分辨率來分別到這幾個文件夾里面去找對應的圖片?! ?/p>
在開發程序時為了兼容不同平臺不同屏幕,建議各自文件夾根據需求均存放不同版本圖片。
我們可以將已經做好的圖片放到該目錄下,或者通過自定義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?! ?/p>
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android中XML文件的使用方法”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。