欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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.添加布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<strong>mybar.xml==></strong>
<?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>
 
<strong>layout_logo.xml==></strong>
<?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>
 
<strong>main.xml==></strong>
<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>

运行效果:

 

posted on   sunwugang  阅读(154)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示