Android Resource

本文详细介绍Android开发中的资源文件,包括Resource的各种类型及其用途。资源文件作为应用的一部分被编译到应用中,支持多种格式如XML、图片、音频等。文章还讲解了资源文件的引用方法及解析方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

Android开发中除了Code之外,资源文件则是开发中经常使用的,本文将系统的介绍Android开发中关于Resource的各种。Android中的资源是在代码中使用的外部文件。这些文件作为应用程序的一部分,被编译到应用程序当中。Android中支持大量的资源文件,如XML文件、图片文件、音频和视频文件。

Resource 分类


目录资源类型
anim/定义动画属性的XML文件。它们被保存在res/anim/文件夹下,通过R.anim类访问
color/定义颜色状态列表的XML文件。它们被保存在res/color/文件夹下,通过R.color类访问
drawable/图片文件,如.png,.jpg,.gif或者XML文件,被编译为位图、状态列表、形状、动画图片。它们被保存在res/drawable/文件夹下,通过R.drawable类访问
layout/定义用户界面布局的XML文件。它们被保存在res/layout/文件夹下,通过R.layout类访问
menu/定义应用程序菜单的XML文件,如选项菜单,上下文菜单,子菜单等。它们被保存在res/menu/文件夹下,通过R.menu类访问
raw/任意的文件以它们的原始形式保存。需要根据名为R.raw.filename的资源ID,通过调用Resource.openRawResource()来打开raw文件
values/包含简单值(如字符串,整数,颜色等)的XML文件。这里有一些文件夹下的资源命名规范。arrays.xml代表数组资源,通过R.array类访问;integers.xml代表整数资源,通过R.integer类访问;bools.xml代表布尔值资源,通过R.bool类访问;colors.xml代表颜色资源,通过R.color类访问;dimens.xml代表维度值,通过R.dimen类访问;strings.xml代表字符串资源,通过R.string类访问;styles.xml代表样式资源,通过R.style类访问
xml/可以通过调用Resources.getXML()来在运行时读取任意的XML文件。可以在这里保存运行时使用的各种配置文件


R类

编译Android应用时,自动生成R类;
该类包含系统中使用的所有资源文件的标识;

资源类:数组array、属性attr、颜色color、图片drawable、ID标识id、布局layout、字符串string;


引用方法

1.资源文件中的引用规则:@[包名称:]资源类型 / 资源名称(在同一个包中引用资源,包名是可选的)

Ex:

<EditText
   android:id=”@+id/myEditText”
   android:layout_width=”fill_parent”
   android:layout_height=”wrap_content”
   android:text=”@string/stop_message”
   android:textColor=”@color/opaque_blue”
  />

2.Java代码中引用的规则:1.获取Resource对象;2.调用Resource实例方法并传递参数(R.resourceTypeName.attributeName)

Ex:

<?xml version="1.0" encoding="utf-8"?>
<resources>
           <string name="app_name">Test Resources</string>
           <string name="test_str1">从代码中引用!</string>
           <string name="test_str2">从资源文件引用!</string>
</resources>
String str = getString(R.string.test_str2).toString();

Ex:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="text_width">150px</dimen>
    <dimen name="text_height">100px</dimen>
    <dimen name="btn_width">30mm</dimen>
    <dimen name="btn_height">10mm</dimen>
</resources> 
// 获得Resources 实例 
       Resources r = getResources(); 
      // 通过getDimension方法获得尺寸值 
       float btn_h = r.getDimension(R.dimen.btn_height); 
       float btn_w = r.getDimension(R.dimen.btn_width); 

解析属性

资源解析主要涉及到两个类,一个是AttributeSet,另一个是TypedArray

AttributeSet

该类位于android.util.AttributeSet,纯粹是一个辅助类,当从XML文件解析时会返回AttributeSet对象,该对象包含了解析元素的所有属性及属性值。并且在解析的属性名称与attrs.xml中定义的属性名称之间建立联系。AttributeSet还提供了一组API接口从而可以方便的根据attrs.xml中已有的名称获取相应的值。

Attribute对象一般作为View的构造函数的参数传递过来:

publlic TextView(Context context,AttributeSet attrs,int defStyle)
AttributeSet中的API可按功能分为以下几类,假定TextView定义如下所示:
<TextView
  android:id="@+id/tv"
  android:layout_width="@dimen/width"
  android:layout_height="wrap_content"
  style="@stylel/text"
/>

操作特定属性:

  • public String getIdAttribute(),获取id属性对应的字符串,此处返回"@+id/tv"
  • public String getStyleAttribute(),获取style属性对应的字符串,返回"@style/text"
  • public int getIdAttributeResourceValue(int defaultValue),返回id属性对应的int值,此处对应R.id.tv。

操作通用属性:

  • public int getAttributeCount(),获取属性的数目,本例中返回4
  • public String getAttributeName(int index),根据属性所在位置返回相应的属性名称。例如,id=0,layout_width=1,layout_height=2,style=3,如果getAttributeName(2),则返回android:layout_height
  • public String getAttributeValue(int index),根据位置返回值。本例中,getAttributeValue(2)则返回"wrap_content"。
  • public String getAttributeValue(String namespace,String name),返回指定命名空间,指定名称的属性值,该方法说明AttributeSet允许给一个XML Element的属性增加多个命名空间的属性值。
  • public int getAttributeResource(int index),返回指定位置的属性id值。本例中,getAttributeResource(2)返回R.attr.layout_width。前面也说过,系统会为每一个attr分配一个唯一的id。

获取特定类型的值:

  • public XXXType getAttributeXXXType(int index,XXXType defaultValue),其中XXXType包括int、unsigned int、boolean、float类型。使用该方法时,必须明确知道某个位置(index)对应的数据类型,否则会返回错误。而且该方法仅适用于特定的类型,如果某个属性值为一个style类型,或者为一个layout类型,那么返回值都将无效。

TypedArray

程序员在开发应用程序时,在XML文件中引用某个变量通常是android:background="@drawable/background",该引用对应的元素一般为某个View/ViewGroup,而View/ViewGroup的构造函数中会通过obatinStyledAttributes方法返回一个TypedArray对象,然后再调用对象中的getDrawable()方法获取背景图片。


TypedArray是对AttributeSet数据类的某种抽象。对于andorid:layout_width="@dimen/width",如果使用AttributeSet的方法,仅仅能获取"@dimen/width"字符串。而实际上该字符串对应了一个dimen类型的数据。TypedArray可以将某个AttributeSet作为参数构造TypedArray对象,并提供更方便的方法直接获取该dimen的值。

TypedArray a = context.obtainStyledAttributes(attrs,com.android.internal.R.styleable.XXX,defStyle,0);

方法obtainStyledAttributes()的第一个参数是一个AttributeSet对象,它包含了一个XML元素中定义的所有属性。第二个参数是前面定义的styleable,appt会把一个styleable编译成一个int[]数组,该数组的内部实现正是通过遍历AttributeSet中的每一个属性,找到用户感兴趣的属性,然后把值和属性经过重定位,返回一个TypedArray对象。想要获取某个属性的值则调用相关的方法即可,比如TypedArray.getDrawbale(),TypedArray.getString()等。getDrawable(),getString()方法内部均通过Resources获取属性值。

资源加载机制

参考:https://mp.youkuaiyun.com/postedit

http://finally-m.iteye.com/blog/2219407


Android resource fraction refers to the ability to specify resource values as fractions or percentages of a parent resource. This is commonly used in Android development to create responsive layouts that adapt to different screen sizes and resolutions. For example, instead of specifying a fixed width or height for a view, you can use resource fractions to define the size relative to the parent container. This allows the view to scale proportionally when the container's size changes. To use resource fractions in Android, you can define them in XML resource files using the "fraction" unit. For instance, you can set the width of a view to half of the parent container by using a fraction value of "0.5". Here's an example of how you can use resource fractions in an XML layout file: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/blue" /> </LinearLayout> ``` In this example, two views are placed inside a LinearLayout with a vertical orientation. Both views have a width defined as half of the parent container, using the "@fraction/0.5" resource value. Using resource fractions can help create more flexible and responsive UI designs in Android applications.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值