android学习笔记32——资源

Android应用资源

资源分类:

1.无法直接访问的原生资源,保存于asset目录下

2.可通过R资源清单类访问的资源,保存于res目录下

资源的类型以及存储方式

android要求在res目录下用不同的子目录来保存不同的应用程序,如下图:

注意:由于对于android中资源的应用方式, 前面的学习当中已有部分使用,因此以下内容只记录,个人认为比较重要或容易遗忘的内容。

内容比较随意,还望谅解!!!

 Android也允许使用资源文件定义boolean常量,需要在res/values/目录下增加一个boolean.xml文件,如下所示:

数组资源

res/values/arrays.xml

arrays.xml可包含以下三种数组:<array.../>、<string-array.../>、<integer-array.../>

<array name="array1">

  <item>@color/c1</item>

  <item>@color/c1</item>

  <item>@color/c1</item>

  ......

</array>

<string-array name="array2">

  <item>@string/c1</item>

  <item>@string/c2</item>

  <item>@string/c3</item>

  ......

</string-array>

<string-array name="books">

  <item>book1</item>

  <item>book2</item>

  <item>book3</item>

  ......

</string-array>

 

Eg:

Drawable资源

res/drawable文件夹,实际开发中应为:drawable-ldpi、drawable-mdpi、drawable-hdpi.

注意:android不允许图片资源文件名中出现大写字母或者以数字开头的文件名称。

StateListDrawable资源

StateListDrawable用于组织多个Drawable对象。

当使用其作为目标组件的背景、前景图片时,StateListDrawable对象所显示的Drawable对象会随目标组件状态的改变而自动切换。

定义StateListDrawable对象的XML文件的根元素为<selector..../>,可包含多个<item>子元素,该元素可指定如下属性:

1.android:color/android:drawable

2.android:state_xxx:指定一个特定状态

实例:实现高亮显示正在输入的文本框

LayerDrawable资源

例如实现自定义SeekBar外观

实例如下所示:

操作步骤:1.添加mybar.xml  2.添加layout_logo.xml  3.添加布局文件

mybar.xml==>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/no"/>
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/plane3"/>

</layer-list>

layout_logo.xml==>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/ten" />
    </item>
    <item android:top="25dp" android:left="25dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/ten" />
    </item>
    <item android:top="50dp" android:left="50dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/ten" />
    </item>

</layer-list>

main.xml==>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progressDrawable="@drawable/mybar" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/layout_logo"/>

</LinearLayout>

运行效果:

原文地址:https://www.cnblogs.com/YYkun/p/5842550.html