系统资源



 


系统资源

资源res

Directory

Resource Type

animator/

XML files that define property animations.

anim/

XML files that define tween animations. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.)

color/

XML files that define a state list of colors. See Color State List Resource

drawable/

Bitmap files (.png.9.png.jpg.gif) or XML files that are compiled into the following drawable resource subtypes:

· Bitmap files

· Nine-Patches (re-sizable bitmaps)

· State lists

· Shapes

· Animation drawables

· Other drawables

See Drawable Resources.

layout/

XML files that define a user interface layout. See Layout Resource.

menu/

XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource.

raw/

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

values/

XML files that contain simple values, such as strings, integers, and colors.

Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename, files in the values/ directory describe multiple resources. For a file in this directory, each child of the <resources> element defines a single resource. For example, a <string> element creates an R.string resource and a <color> element creates an R.color resource.

Because each resource is defined with its own XML element, you can name the file whatever you want and place different resource types in one file. However, for clarity, you might want to place unique resource types in different files. For example, here are some filename conventions for resources you can create in this directory:

· arrays.xml for resource arrays (typed arrays).

· colors.xml for color values

· dimens.xml for dimension values.

· strings.xml for string values.

· styles.xml for styles.

See String ResourcesStyle Resource, and More Resource Types.

xml/

Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various XML configuration files must be saved here, such as a searchable configuration.

Caution: Never save resource files directly inside the res/ directoryit will cause a compiler error.

 

 

1.主题theme和控件风格style.xml

位于values/style

 

(1)窗体theme继承的主题--theme.xml

各种系统主题链接

http://henzil.easymorse.com/?p=364

设置各种控件的各种属性,如窗体的样式,背景色

<resources>

    <style name="AppTheme" parent="AppBaseTheme">

    </style>

</resources>

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

 <style name="CustomTheme" parent = @android:style/Theme.Dialog>

    <item name="android:windowNoTitle">true</item>

    <item name="windowFrame">@drawable/screen_frame</item>

    <item name="windowBackground">@drawable/screen_background_white</item>

 </style>

</resources>

示例:

如果你喜欢一个主题,但是想做一些轻微的改变,你只需要将这个主题添加为父主题。比如我们修改Theme.Dialog主题。我们来继承Theme.Dialog来生成一个新的主题。
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
继承了Theme.Dialog后,我们可以按照我们的要求来调整主题。我们可以修改在Theme.Dialog中定义的每个item元素的值,然后我们在Android Manifest 文件中使用CustomDialogTheme 而不是 Theme.Dialog 
2.在程序当中设置主题如果需要的话,你可 以在Activity当中通过使用方法setTheme()来加载一个主题。注意,如果你这么做的话,你应该初始化任何View之前设置主题。比如,在调 用setContentView(View) inflate(int, ViewGroup)方法前。这保证系统将当前主题应用在所有的UI界面。例子如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.linear_layout_3);
}
如果你打算在程序代码中来加载主界面的主题,那么需要注意主题当中不能包括任何系统启动这个Activity所使用的动画,这些动画将在程序启动前显示。在很多情况下,如果你想将主题应用到你的主界面,在XML中定义似乎是一个更好的办法。

 

 

 

注:由parent标签标示继承父主题,在Activity中由androidtheme引用主题

(2)控件stylle--style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SpecialText" parent="@style/Text">
     <item name="android:textSize">18sp</item>
     <item name="android:textColor">#008</item>
</style>
</resources>
如上所示,你可以用<item>元素来为你的风格定义一组格式化的值。
注意在<style>元素中的parent,由此可继承已有控件风格,并在其基础上改变。你可以从任何包 含这个风格的资源当中继承此风格。通常上,你的资源应该一直直接或者间接地继承Android的标准风格资源。 这样的话,你就只需要定义你想改变的值。

2.Drawable对应res/drawable

1.layer-list

layer-list是一个存储drawable的数组,其中按数组顺序进行图层排序,所以数组中最大的一个元素即在最顶层。 

官方解释

LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the listthe last drawable in the list is drawn on top.

Each drawable is represented by an <item> element inside a single <layer-list> element.

file location:

res/drawable/filename.xml
The filename is used as the resource ID.

compiled resource datatype:

Resource pointer to a LayerDrawable.

resource reference:

In Java: R.drawable.filename
In XML: @[package:]drawable/filename

syntax:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</layer-list>

elements:

<layer-list>

Required. This must be the root element. Contains one or more <item> elements.

attributes:

xmlns:android

StringRequired. Defines the XML namespace, which must be"http://schemas.android.com/apk/res/android".

<item>

Defines a drawable to place in the layer drawable, in a position defined by its attributes. Must be a child of a <selector> element. Accepts child <bitmap> elements.

attributes:

android:drawable

Drawable resourceRequired. Reference to a drawable resource.

android:id

Resource ID. A unique resource ID for this drawable. To create a new resource ID for this item, use the form: "@+id/name". The plus symbol indicates that this should be created as a new ID. You can use this identifier to retrieve and modify the drawable with View.findViewById() orActivity.findViewById().

android:top

Integer. The top offset in pixels.

android:right

Integer. The right offset in pixels.

android:bottom

Integer. The bottom offset in pixels.

android:left

Integer. The left offset in pixels.

All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate. To avoid scaling items in the list, use a <bitmap> element inside the <item> element to specify the drawable and define the gravity to something that does not scale, such as "center". For example, the following <item> defines an item that scales to fit its container View:

<item android:drawable="@drawable/image" />

To avoid scaling, the following example uses a <bitmap> element with centered gravity:

<item>
  <bitmap android:src="@drawable/image"
          android:gravity="center" />
</item>

example:

XML file saved at res/drawable/layers.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/android_red"
        android:gravity="center" />
    </item>
    <item android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/android_green"
        android:gravity="center" />
    </item>
    <item android:top="20dp" android:left="20dp">
      <bitmap android:src="@drawable/android_blue"
        android:gravity="center" />
    </item>
</layer-list>

Notice that this example uses a nested <bitmap> element to define the drawable resource for each item with a "center" gravity. This ensures that none of the images are scaled to fit the size of the container, due to resizing caused by the offset images.

This layout XML applies the drawable to a View:

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

The result is a stack of increasingly offset images:

 

 

2定义控件的形状shape.xml

位于drawable/shape.xml

 

官方文档

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" "oval" "line" "ring">
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" "radial" "sweep"]
        android:useLevel=["true" "false"/>
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

elements:

<shape>

形状.必须为根元素

attributes:

xmlns:android

String. Required.定义在xmlnamespace, 必须为"http://schemas.android.com/apk/res/android".

android:shape

关键字。定义这个形状的类型,可定义形状如下:

Value

Desciption

"rectangle"

A rectangle that fills the containing View. This is the default shape.

"oval"

An oval shape that fits the dimensions of the containing View.

"line"

A horizontal line that spans the width of the containing View. This shape requires the <stroke> element to define the width of the line.

"ring"

A ring shape.

下面的属性只被用于 android:shape="ring":

android:innerRadius

Dimension. 圆环的内部半径(中部的洞),可做为一个dimension值或dimension resource

android:innerRadiusRatio

Float. The radius for the inner part of the ring, expressed as a ratio of the ring's width. For instance, if android:innerRadiusRatio="5", then the inner radius equals the ring's width divided by 5. This value is overridden by android:innerRadius. Default value is 9.

android:thickness

Dimension. The thickness of the ring, as a dimension value or dimension resource.

android:thicknessRatio

Float. The thickness of the ring, expressed as a ratio of the ring's width. For instance, ifandroid:thicknessRatio="2", then the thickness equals the ring's width divided by 2. This value is overridden by android:innerRadius. Default value is 3.

android:useLevel

Boolean. "true" if this is used as a LevelListDrawable. This should normally be "false" or your shape may not appear.

<corners>

为shape创建圆角,只被用于shape是一个rectangle

属性:

android:radius

Dimension.所有角的弧半径, 是一个dimension或者是一个dimension resource.可以被以下几个属性覆盖。a

android:topLeftRadius

Dimension. 左上角。

android:topRightRadius

Dimension. 右上角。

android:bottomLeftRadius

Dimension. 左下角。

android:bottomRightRadius

Dimension.右下角。

Note:每个角的半径必须大于1,或者没有规定圆角。如果想指定角不是圆角,可以用androidradius设置一个默认大于1的圆角弧 ,但是在后面覆盖你想的值,如果不想设定可以用0.

<gradient>

指定一个渐变色。

attributes:

android:angle

Integer.渐变角度,0--左到右,90--底到上。必须是45的倍数,默认是0.

android:centerX

Float.渐变中心的X相对位置。(0-1.0)。

android:centerY

Float. 渐变中心的Y相对位置。(0-1.0)。

android:centerColor

Color.任选的界于开始于结束瑟之间的颜色色, as a hexadecimal value orcolor resource.

android:endColor

Color. The ending color, as a hexadecimal value or color resource.

android:gradientRadius

Float. The radius for the gradient. Only applied when android:type="radial".

android:startColor

Color. The starting color, as a hexadecimal value or color resource.

android:type

Keyword. 所应用的渐变模式类型。包括以下:

Value

Description

"linear"

A linear gradient. This is the default.

"radial"

A radial gradient. The start color is the center color.

"sweep"

A sweeping line gradient.

android:useLevel

Boolean. "true" if this is used as a LevelListDrawable.

<padding>

Padding to apply to the containing View element (this pads the position of the View content, not the shape).

attributes:

android:left

Dimension. Left padding, as a dimension value or dimension resource.

android:top

Dimension. Top padding, as a dimension value or dimension resource.

android:right

Dimension. Right padding, as a dimension value or dimension resource.

android:bottom

Dimension. Bottom padding, as a dimension value or dimension resource.

<size>

Shape的大小

属性:

android:height

Dimension. The height of the shape, as a dimension value or dimension resource.

android:width

Dimension. The width of the shape, as a dimension value or dimension resource.

Note: The shape scales to the size of the container View proportionate to the dimensions defined here, by default. When you use the shape in an ImageView, you can restrict scaling by setting theandroid:scaleType to "center".

<solid>

Shape的填充色。

attributes:

android:color

Color. The color to apply to the shape, as a hexadecimal value or color resource.

<stroke>

Shape的描边。

属性:

android:width

Dimension. The thickness of the line, as a dimension value or dimension resource.

android:color

Color. The color of the line, as a hexadecimal value or color resource.

android:dashGap

Dimension. The distance between line dashes, as a dimension value or dimension resource. Only valid if android:dashWidth is set.

android:dashWidth

Dimension. The size of each dash line, as a dimension value or dimension resource. Only valid ifandroid:dashGap is set.

example:

XML file saved at res/drawable/gradient_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

This layout XML applies the shape drawable to a View:

<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

This application code gets the shape drawable and applies it to a View:

Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

Shape1.xml

 

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient 

        android:startColor="#FFFFFF"

        android:endColor="#DC143C"

        android:angle="45"

        android:type="linear"/>

    <corners

        android:radius="40dp"/>

    <stroke

        android:width="1dp"

        android:color="#000000"

      />

    <padding android:left="2dp"

        android:top="2dp"

        android:right="2dp"

        android:bottom="2dp"/>

   

</shape>

关于androidangle xoy坐标看,45为顺向右上方渐变

 

 

 

 

 3.尺寸dimens.xml

 

<resources>

    <dimen name="notepad_margin">30dp</dimen>

</resources>

4.国际化string.xml

注:限定符(1language:小写字母(en/ch

           (2)Regionr后加2个大写字母:(rUS

5.attr.xml属性文档,供控件调用的属性

1)原理:在attrs.xml中添加控件在xml中可以设置的属性集合如<attr  name = “android:text”  format = “String”>属性,则我们在构建layout是调用自定义的控件时就可以调用在attrs.xml中所添加的属性如android:text = hellow android.当然在attrs.xml中定义的属性必须要有相应的基本控件来设置和获取。如text属性可以由一个TextView来实现,并提供setget.

2)属性格式

name是属性名,format是属性的格式,有如下几种格式

reference---引用

color----颜色

boolean---布尔值

dimension---尺寸

float----浮点值

integer---整形值

string---字符串

fraction----百分数

enum

<attr name = “orientation”>

<enum  name = “horizondtal” value = “0”/>

<enum  name = “vertical ” value = “1”/>

</attr>

3)自定义命名空间:xmlns[myspace]=http://schemas;android.com/apk/res/包名”

包名是Manifest中的<manifest>节点处的包名

调用自定义命名空间的属性需要添加空间名如myspacetext

4)如何获取属性的值

A)TypedArray a = context.obtainStyledAttributes();

B)由TypedArraygetXXX()获取相应的值,如getText(),getDrawable();

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值