strings.xml文件,定義一些需要在開發(fā)中使用的字..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android常用的資源文件--values文件夾內文件

發(fā)布時間:2020-06-29 15:05:40 來源:網絡 閱讀:1628 作者:詩情碧霄 欄目:移動開發(fā)


Android資源文件--values夾下文件及用法


以下文件皆在values文件夾下。例如:全路徑:res/values/string.xml。


I>strings.xml文件,定義一些需要在開發(fā)中使用的字符串變量和數組,用來實現國際化,使用方法分別為:R.string.自己命名的名稱、@string/自己命名的名稱。

<resources>
        <!--屬性name="自己命名的名稱"-->
    <string name="app_name">Android4.0</string>
    <string_array name="ball">
        <item name="basketball">籃球</item>
        <item name="soccer">足球</item>
</resources>

使用舉例:

在xxx.java文件中使用方法:getResource().getString(R.string.app_name);

在xxx.xml文件中使用方法:android:text=“@string/app_name”


II>array.xml文件,定義存放一些數組的內容,使用方法同上。

<resources>
    <array name="color">
        <item>#000</item>
        <item>#fff</item>
    </array>
</resources>

使用舉例:

在xxx.java文件中使用方法:getResource().getStringArray(R.arry.color);

在xxx.xml文件中使用方法:android:entries=“@array/color”(注:為spinner添加數組初始值)


III>colors.xml文件,主要存放一些自定義的顏色,使用方法同上。

<resources>
    <color name="RED">#f00</color>
</resources>

使用舉例:

在xxx.java文件中使用方法:getResource().getColor(R.color.RED);

在xxx.xml文件中使用方法:android:background=“@color/RED”


IV>dimens.xml文件,主要定義一些尺寸,使用方法同上。

<resources>
    <dimen name="horizontal_margin">15dp</dimen>
</resources>

使用舉例:

在xxx.xml文件中使用方法:android:background=“@dimens/horizontal_margin”

尺寸的單位:

符號名稱用法

dp

獨立像素

與設備大小無關

dx像素與設備大小無關
sp放大像素一般用于設置文字的大小

長度轉換:1 pt = 1/72 in

長度單位:

符號名稱
pt
in英寸
mm毫米
cm厘米


V>bools.xml文件,定義一個布爾型的文件,使用方法同上。

<resources>
    <bool name="flag_on">true</bool>
</resources>


VI>styles.xml文件,放置樣式的文件,可以使自己定義的樣式,也可存放法系統(tǒng)的樣式,樣式可以應用用在窗口、控件、布局、主題設置中,但是必須與控件(View)的屬性保持一致。定義樣式分為兩種:


格式一:

<style name="定義當前的樣式/主題的名稱(主要用于引用)">

    <item name="屬性名稱">屬性值</item>

</style>

<style name="mystyle">
  <item name="android:layout_width">match_parent</item>
  <itme name="android:textSize">30sp</item>
</style>

使用舉例:

在xxx.xml文件中使用方法:android:style=“@style/mystyle”

在清單文件中使用:

<application 

    theme="@style/mystyle"

    ...>

    ...

</application>

...


格式二:

<style name="定義當前的樣式/主題的名稱(主要用于引用)" parent=“父樣式名稱”>

    <item name="屬性名稱">屬性值</item>

</style>


<style name="mystylew2" parent="mystyle">
        <item name="android:textcolor">#0f0</item>
</style>
<style name="mystyle.mystyle2">
        </item name="android:textColor">#0f0</item>
</style>

使用舉例:

在xxx.xml文件中使用方法:android:style=“@style/mystyle”

在清單文件中使用:

<application 

    theme="@style/mystyle"

    ...>

    ...

</application>

...


總結:以上為values文件夾下常用的文件屬性的設置,還用其他的屬性,通常在屬性賦值的時候直接進行賦值,降低消耗,提高效率。常用的屬性通過調用名稱賦值,方便代碼的維護,簡化代碼,避免出現“重復造輪”的現象。以上都是自己命名并設置的屬性,其實android系統(tǒng)本身也為開發(fā)者提供了一些屬性,例如:@android:color/darker_gray 調用系統(tǒng)提供的暗灰色。這一獲取方式僅僅獲取系統(tǒng)提供的顏色,樣式的獲取也可以采用這種方法式,至于詳細的講解,以后再繼續(xù)補充。希望對大家的學習和開發(fā)能有用,不足之處請大家不起賜教,謝謝。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI