Android的Resource

  Resource主要用于UI,通过它可以尽可能地把界面设计与逻辑编码分离开,减少关联性。
  这种Resource独立于代码的编程方式很早就有了,像VC可以把控件属性放在RC文件中,Java的ResourceBundle工具类可以从property file里读取属性值,GTKBuilder可以从XML文件中读取控件属性等等。Android主要是为此制定了一系列的规则,像什么资源该用什么形式来表达,什么资源该放在什么目录之下这些都统统规定好了,违反规则的将编译通不过。譬如把一张图片放到res/layout下,编译器就会报错。还有Alternative资源,目录名字的取法、各种Qualifier的顺序也都是有规定的,随便取个目录名将造成编译出错(好在有ADT辅助,让这项工作简单不少)。
  在资源类型方面,Android选择主要使用XML文件,比起简单的Key-Value对,XML能够表达复杂的东西,而且XML发展到今天,解析技术已经很成熟了。当然图片也是常用的资源。

  设计资源的时候,主要可以考虑以下几个方面:
  • 国际化
  如果支持多国用户,就得考虑各国语言在文字上的差异,以及数字、货币、日期等方面的不同表示方式。这个差异除了字符串翻译上的,还有布局上的。譬如有的语言文字从左到右,有的语言文字从右到左;有的国家姓在前,名在后,有的国家则相反,等等。
  • 兼容性
  现在Android支持多种配置的手机,譬如屏幕就有好几种尺寸的。虽然系统会自动进行一些兼容性处理(自动缩放或者放在正中间之类),但是那肯定不是最优的。所以为了达到更好的用户体验,可以针对不同的屏幕做一些特殊优化,例如提供不同分辨率的背景图片之类。
  • 简单化
  Android尽可能的把一些代码转化成能用Resource表示。比较明显的是像ColorStateList、StateListDrawable这种,本来需要代码里做个判断,然后设置不同的资源,现在在Resource里直接设置就可以了。当然相关的代码都放到Framework那一层去做了。所以可以利用一下这些,简化自己的代码。


  使用Resource的方式:

  a)代码里
  很多API支持Resource ID的参数方式,传入ID即可。ID的形式是[<package_name>.]R.<resource_type>.<resource_name>
  放在assets/目录下的,可以使用Resources类的getAssets()方法得到AssetManager,然后打开读取文件内容。
  放在res/raw/目录下的,可以使用Resources类的openRawResource()方法得到InputStream,然后读取文件内容。

  b)XML文件里
  一般引用,采用@[<package_name>:]<resource_type>/<resource_name>的形式;
  引用style属性时,采用?[<package_name>:][<resource_type>/]<resource_name>的形式

  另外,还有些有用的使用方式:使用<include>标签把其他的Layout包含进来(可以与<merge>配合使用);使用<bitmap>标签,通过android:src属性指定真正的图片,可以用XML来模拟图片之类。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值