- 浏览: 57650 次
- 性别:
- 来自: 成都
文章分类
最新评论
1.Android 在XMl里面共享同一布局文件
一直以为共享同一布局文件都是在之前写好的布局之后复制过来,或者通过 java代码使用layoutInflater Add进来的。但今天提供了一个更为方便的使用方法,可以在你的任意LAYOUT文件里面将其他的LAYOUT文件拿过来使用,代码也很简单,如下:
<?xml version="1.0" encoding="UTF-8"?><id android:background="@drawable/layout_background" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"><text android:textcolor="#f0f0f0" android:layout_width="wrap_content" android:layout_height="wrap_content">
这是我定义的公用layout 文件,我给他起名为:title
然后我们在另外的布局文件如下使用:
<includelayout></includelayout>
即可将title 的布局直接拿到我们当前的布局文件中。
Tip:使用include标记将layout 放入我们当前的layout文件,也可以直接使用当前 view .findViewByid得到title 布局文件中的任意View 。
2.使用XML的方式为背景添加背景渐变的效果及控件美化
首先,在res/Drawable 文件夹里面添加一个jbshape.xml文件,然后写入如下代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><solid android:color="#ff4100ff"></solid><gradient android:startcolor="#ff8c00" android:endcolor="#FFFFFF" android:angle="270"></gradient><stroke android:width="2dp" android:color="#dcdcdc"></stroke><corners android:radius="2dp"></corners><padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp"></padding></shape>
shape 节点配置的是图形的形式,主要包括方形、圆形等,默认为"rectangle"方形;
gradient 节点主要配置起点颜色、终点颜色及中间点的颜色、坐标、渐变效果(0,90,180从左到右渐变,270从上到下渐变)默认从左到右;
<solid android:color="#ff4100ff"></solid>实心的,填充里面;
<stroke android:dashwidth="3dp" android:dashgap="2dp" android:width="2dp" android:color="#FF00ff00" dashwidth dashgap width="2dp" padding corners version="”1.0″" encoding="”UTF-8″?"><linearlayout xmlns:android="”http://schemas.android.com/apk/res/android”" android:orientation="”vertical”" android:layout_width="”fill_parent”" android:layout_height="”fill_parent”" android:paddingleft="”0px”">
3.如何用代码自定义Android 自动生成的标题?
在onCreate中加入以下代码:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
这3句代码的顺序不可以颠倒。
原文链接:http://www.cnblogs.com/xirihanlin/archive/2010/06/14/1758145.html
Drawable资源
Drawable资源是对图像的一个抽象,你可以通过getDrawable(int)得到并绘制到屏幕上。这里有几种不同类型的Drawable:
Bitmap File
一个Bitmap图像文件(.png、.jpg或.gif)。BitmapDrawable。
Nine-Patch File
一个带有伸缩区域的PNG文件,可以基于content伸缩图片(.9.png)。NinePatchDrawable。
State List
一个XML文件,为不同的状态引用不同的Bitmap图像(例如,当按钮按下时使用不同的图片)。StateListDrawable。
Color
定义在XML中的资源,指定一个矩形(圆角可以有)的颜色。PaintDrawable。
Shape
一个XML文件,定义了一个几何形状,包括颜色和渐变。ShapeDrawable。
AnimationDrawable资源的说明在【Animation资源】文章中。
Bitmap File
基本的Bitmap图像。Android支持几种不同格式的Bitmap文件:.png(最佳)、.jpg(可接受)、.gif(不要)。
注意:Bitmap文件可能会被aapt工具进行无损图像压缩优化。例如,一个真彩色的PNG(不超过256色)可能会被转换成一个带有颜色板的8位PNG。这样做能保证图片质量一样,但减少内存占用。因此,需要了解的是放在这个文件夹下的二进制图像在编译时可能会发生变更。如果你打算以位流方式读取图像来转化成Bitmap的话,可以把它们放到res/raw文件中,在这里,它们不会被优化。
File Location:
res/drawable/filename.png (.png, .jpg, 或.gif)
文件名会被当作资源ID使用。
Complied Resource Datatype:
指向BitmapDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Example:
在res/drawable/myimage.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:
<imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/myimage"></imageview>
下面的代码可以以Drawable方式得到图片:
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);
Nine-Patch File
NinePatch是一种PNG图像,可以定义拉伸区域,当View的content超出图像边界的话,Android会拉伸它。典型用法是把这个图像设置为View的背景,而这个View至少有一个尺寸设置为“wrap_content”,当这个View变大来容纳content时,Nine-Patch图像也会拉伸来匹配View的大小。
File Location:
res/drawable/filename.9.png
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向NinePatchDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Example:
在res/drawable/myninepatch.9.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:
<button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/myninepatch"></button>
State List
StateListDrawable是定义在XML中的Drawable对象,能根据状态来呈现不同的图像。例如,Button存在多种不同的状态(pressed、focused或other),使用StateListDrawable,你可以为Button的每个状态提供不同的按钮图像。
你可以在XML文件中描述状态列表。在<selector>元素里的每个<item>代表每个图像。每个<item>使用不同的特性来描述使用的时机。
当每次状态改变时,StateList都会从上到下遍历一次,第一个匹配当前状态的item将被使用——选择的过程不是基于“最佳匹配”,只是符合state的最低标准的第一个item。
File Location
Res/drawable/filename.xml
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向StateListDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantsize='["true"' android:dither='["true"' android:variablepadding='["true"'><item android:drawable="@[package:]drawable/drawable_resource" android:state_pressed='["true"' android:state_focused='["true"' android:state_selected='["true"' android:state_active='["true"' android:state_checkable='["true"' android:state_checked='["true"' android:state_enabled='["true"' android:state_window_focused='["true"'></item></selector>
Elements:
<selector>
必须。必须是根元素。可以包含一个或多个<item>元素。
Attributes:
xmlns:android
String。必须。定义XML的命名空间,必须是
“http://schemas.android.com/apk/res/android”。
android:constantSize
Boolean。“true”表示随着状态变化,Drawable的大小保持不变(所有状态中最大的size);“false”表示大小会变化。默认是false。
android:dither
Boolean。“true”表示当Bitmap和屏幕的不是相同的像素设定时支持Bitmap抖动(例如,ARGB 8888的Bitmap和RGB 565的屏幕);“false”表示不支持。默认是“true”。
android:variablePadding
Boolean。“true”表示Drawable的Padding可以变化;“false”表示Padding保持相同(所有状态的最大Padding)。使能这一特征需要在状态变化时处理Layout,一般都不支持。默认值是false。
<item>
定义特定状态的Drawable,通过它的特性指定。必须是<selector>的子元素。
Attributes:
android:drawable
Drawable资源。必须。指向一个Drawable资源。
android:state_pressed
Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。
android:state_focused
Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。
android:state_selected
Boolean。“true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。
android:state_checkable
Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)
android:state_checked
Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。
android:state_enabled
Boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。
android:window_focused
Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。
注意:记住一点,StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。
Example:
XML文件保存在res/drawable/button.xml。
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/button_pressed"></item><!-- pressed --><item android:state_focused="true" android:drawable="@drawable/button_focused"></item><!-- focused --><item android:drawable="@drawable/button_normal"></item><!-- default --></selector>
Layout XML将这个Drawable应用到一个View上:
<imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/button"></imageview>
Color
定义在XML中的color,可以当作Drawable使用,来填充矩形区域(圆角可以有)。这种Drawable的行为很像是颜色填充。
注意:Color Drawable是一种简单的资源,可以使用name特性来引用其值(不再是XML文件的名)。因此,你可以在一个XML文件中的<resources>元素下添加多个Color Drawable。
File Location:
res/drawable/filename.xml
文件名随意。元素的name将会当作资源ID使用。
Complied Resource Datatype:
指向PaintDrawable资源的指针。
Resource Reference:
R.drawable.color_name(Java)
@[package:]drawable/color_name(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><resources><drawable name="color_name">color</drawable></resources>
Elements:
<resources>
必须。必须是根节点。
没有特性。
<drawable>
一个color Drawable。其值可以是任何有效的十六进制颜色值或者Color资源。Color值总是以“#”开头,后面紧跟Alpha-Red-Green-Blue信息,格式是:#RGB、#ARGB或者#AARRGGBB。
Attributes:
name
String。必须。Color的名字。这个名字将被当作资源ID使用。
Example:
XML文件保存在res/drawable/color.xml。
<?xml version="1.0" encoding="utf-8"?><resources><drawable name="solid_red">#f00</drawable><drawable name="solid_blue">#0000ff</drawable></resources>
Layout XML将会把这个Color Drawable应用到一个View上:
<textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/solid_blue"></textview>
代码中获取Color Drawable并应用到View上:
Resources res = getResources();
Drawable redDrawable = res.getDrawable(R.drawable.solid_red);
TextView tv = (TextView) findViewByID(R.id.text);
tv.setBackground(redDrawable);
Shape
定义在XML中的几何形状。
File Location:
res/drawable/filename.xml
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向ShapeDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape='["rectangle"'><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"' android:useslevel='["true"'></gradient><solid android:color="color"></solid><stroke android:width="integer" android:color="color" android:dashwidth="integer" android:dashgap="integer"></stroke><padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer"></padding><corners android:radius="integer" android:topleftradius="integer" android:toprightradius="integer" android:bottomleftradius="integer" android:bottomrightradius="integer"></corners></shape>
Elements:
<shape>
必须。必须是根元素。
Attributes:
android:shape
Keyword。定义Shape的类型。有效的值包括:
Value
Desciption
"rectangle"
矩形。默认形状。
"oval"
椭圆。
"line"
水平直线。需要<stroke>元素定义线的宽度。
"ring"
环形。
接下来的特性只能在android:shape=”ring”时使用:
android:innerRadius
Dimension。内环的半径。
android:innerRadiusRatio
Float。以环的宽度比率来表示内环的半径。例如,如果android:innerRadiusRatio=”5”,内环半径等于环的宽度除以5。这个值可以被android:innerRadius覆盖。默认值是9。
android:thickness
Dimension。环的厚度。
android:thicknessRatio
Float。以环的宽度比率来表示环的厚度。例如,如果android:thicknessRatio=”2”,厚度就等于环的宽度除以2。这个值可以被android:thickness覆盖。默认值是3。
android:useLevel
Boolean。“true”表示可以当作LevelListDrawable使用。一般都为“false”。
<gradient>
为Shape指定渐变色。
Attributes:
android:angle
Integer。渐变色的角度值。0表示从左到右,90表示从下到上。必须是45的倍数,默认是0。
android:centerX
Float。渐变色中心的X相对位置(0-1.0)。当android:type=”linear”时无效。
android:centerY
Float。渐变色中心的Y相对位置(0-1.0)。当android:type=”linear”时无效。
android:centerColor
Color。可选的颜色,出现在start和end颜色之间。
android:endColor
Color。end颜色。
android:gradientRadius
Float。渐变色的半径。当android:type=”radial”时有效。
android:startColor
Color。start颜色。
android:type
Keyword。渐变色的样式。有效值为:
Value
Description
"linear"
线性渐变,默认值。
"radial"
环形渐变。start颜色是处于中间的颜色。
"sweep"
sweep渐变
android:useLevel
Boolean。“true”表示可以当作LevelListDrawable使用。
<solid>
填充shape的单一色。
Attributes:
android:color
Color。这个颜色会应用到shape上。
<stroke>
shape的线形。
Attributes:
android:width
Dimension。线的厚度。
android:color
Color。线的颜色。
android:dashGap
Dimension。间断线间的距离。仅在android:dashWidth设定时有效。
android:dashWidth
Dimension。间断线的大小。仅在android:dashGap设定时有效。
<padding>
内部View元素的边距。
Attributes:
android:left
Dimension。左内边距。
android:top
Dimension。上内边距。
android:right
Dimension。右内边距。
android:bottom
Dimension。下内边距。
<corners>
为shape创建圆角。当shape是一个矩形时有效。
Attributes:
android:radius
Dimension。圆角的半径。会被下面的特性覆盖。
android:topLeftRadius
Dimension。左上圆角半径。
android:topRightRadius
Dimension。右上圆角半径。
android:bottomLeftRadius
Dimension。左下圆角半径。
android:bottomRightRadius
Dimension。右下圆角半径。
Examples:
XML文件保存在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"></gradient><padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp"></padding><corners android:radius="8dp"></corners></shape>
Layout XML将被当作ShapeDrawable应用到一个View上:
<textview android:background="@drawable/gradient_box" android:layout_height="wrap_content" android:layout_width="wrap_content"></textview>
代码中也可以获得ShapeDrawable,然后应用到View上:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);</corners></padding></stroke></solid></gradient></stroke></shape></drawable></resources></resources></selector></item></item></selector></item></item></selector></linearlayout></stroke></text></id>
一直以为共享同一布局文件都是在之前写好的布局之后复制过来,或者通过 java代码使用layoutInflater Add进来的。但今天提供了一个更为方便的使用方法,可以在你的任意LAYOUT文件里面将其他的LAYOUT文件拿过来使用,代码也很简单,如下:
<?xml version="1.0" encoding="UTF-8"?><id android:background="@drawable/layout_background" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"><text android:textcolor="#f0f0f0" android:layout_width="wrap_content" android:layout_height="wrap_content">
这是我定义的公用layout 文件,我给他起名为:title
然后我们在另外的布局文件如下使用:
<includelayout></includelayout>
即可将title 的布局直接拿到我们当前的布局文件中。
Tip:使用include标记将layout 放入我们当前的layout文件,也可以直接使用当前 view .findViewByid得到title 布局文件中的任意View 。
2.使用XML的方式为背景添加背景渐变的效果及控件美化
首先,在res/Drawable 文件夹里面添加一个jbshape.xml文件,然后写入如下代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><solid android:color="#ff4100ff"></solid><gradient android:startcolor="#ff8c00" android:endcolor="#FFFFFF" android:angle="270"></gradient><stroke android:width="2dp" android:color="#dcdcdc"></stroke><corners android:radius="2dp"></corners><padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp"></padding></shape>
shape 节点配置的是图形的形式,主要包括方形、圆形等,默认为"rectangle"方形;
gradient 节点主要配置起点颜色、终点颜色及中间点的颜色、坐标、渐变效果(0,90,180从左到右渐变,270从上到下渐变)默认从左到右;
<solid android:color="#ff4100ff"></solid>实心的,填充里面;
<stroke android:dashwidth="3dp" android:dashgap="2dp" android:width="2dp" android:color="#FF00ff00" dashwidth dashgap width="2dp" padding corners version="”1.0″" encoding="”UTF-8″?"><linearlayout xmlns:android="”http://schemas.android.com/apk/res/android”" android:orientation="”vertical”" android:layout_width="”fill_parent”" android:layout_height="”fill_parent”" android:paddingleft="”0px”">
3.如何用代码自定义Android 自动生成的标题?
在onCreate中加入以下代码:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
这3句代码的顺序不可以颠倒。
原文链接:http://www.cnblogs.com/xirihanlin/archive/2010/06/14/1758145.html
Drawable资源
Drawable资源是对图像的一个抽象,你可以通过getDrawable(int)得到并绘制到屏幕上。这里有几种不同类型的Drawable:
Bitmap File
一个Bitmap图像文件(.png、.jpg或.gif)。BitmapDrawable。
Nine-Patch File
一个带有伸缩区域的PNG文件,可以基于content伸缩图片(.9.png)。NinePatchDrawable。
State List
一个XML文件,为不同的状态引用不同的Bitmap图像(例如,当按钮按下时使用不同的图片)。StateListDrawable。
Color
定义在XML中的资源,指定一个矩形(圆角可以有)的颜色。PaintDrawable。
Shape
一个XML文件,定义了一个几何形状,包括颜色和渐变。ShapeDrawable。
AnimationDrawable资源的说明在【Animation资源】文章中。
Bitmap File
基本的Bitmap图像。Android支持几种不同格式的Bitmap文件:.png(最佳)、.jpg(可接受)、.gif(不要)。
注意:Bitmap文件可能会被aapt工具进行无损图像压缩优化。例如,一个真彩色的PNG(不超过256色)可能会被转换成一个带有颜色板的8位PNG。这样做能保证图片质量一样,但减少内存占用。因此,需要了解的是放在这个文件夹下的二进制图像在编译时可能会发生变更。如果你打算以位流方式读取图像来转化成Bitmap的话,可以把它们放到res/raw文件中,在这里,它们不会被优化。
File Location:
res/drawable/filename.png (.png, .jpg, 或.gif)
文件名会被当作资源ID使用。
Complied Resource Datatype:
指向BitmapDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Example:
在res/drawable/myimage.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:
<imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/myimage"></imageview>
下面的代码可以以Drawable方式得到图片:
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);
Nine-Patch File
NinePatch是一种PNG图像,可以定义拉伸区域,当View的content超出图像边界的话,Android会拉伸它。典型用法是把这个图像设置为View的背景,而这个View至少有一个尺寸设置为“wrap_content”,当这个View变大来容纳content时,Nine-Patch图像也会拉伸来匹配View的大小。
File Location:
res/drawable/filename.9.png
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向NinePatchDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Example:
在res/drawable/myninepatch.9.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:
<button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/myninepatch"></button>
State List
StateListDrawable是定义在XML中的Drawable对象,能根据状态来呈现不同的图像。例如,Button存在多种不同的状态(pressed、focused或other),使用StateListDrawable,你可以为Button的每个状态提供不同的按钮图像。
你可以在XML文件中描述状态列表。在<selector>元素里的每个<item>代表每个图像。每个<item>使用不同的特性来描述使用的时机。
当每次状态改变时,StateList都会从上到下遍历一次,第一个匹配当前状态的item将被使用——选择的过程不是基于“最佳匹配”,只是符合state的最低标准的第一个item。
File Location
Res/drawable/filename.xml
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向StateListDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantsize='["true"' android:dither='["true"' android:variablepadding='["true"'><item android:drawable="@[package:]drawable/drawable_resource" android:state_pressed='["true"' android:state_focused='["true"' android:state_selected='["true"' android:state_active='["true"' android:state_checkable='["true"' android:state_checked='["true"' android:state_enabled='["true"' android:state_window_focused='["true"'></item></selector>
Elements:
<selector>
必须。必须是根元素。可以包含一个或多个<item>元素。
Attributes:
xmlns:android
String。必须。定义XML的命名空间,必须是
“http://schemas.android.com/apk/res/android”。
android:constantSize
Boolean。“true”表示随着状态变化,Drawable的大小保持不变(所有状态中最大的size);“false”表示大小会变化。默认是false。
android:dither
Boolean。“true”表示当Bitmap和屏幕的不是相同的像素设定时支持Bitmap抖动(例如,ARGB 8888的Bitmap和RGB 565的屏幕);“false”表示不支持。默认是“true”。
android:variablePadding
Boolean。“true”表示Drawable的Padding可以变化;“false”表示Padding保持相同(所有状态的最大Padding)。使能这一特征需要在状态变化时处理Layout,一般都不支持。默认值是false。
<item>
定义特定状态的Drawable,通过它的特性指定。必须是<selector>的子元素。
Attributes:
android:drawable
Drawable资源。必须。指向一个Drawable资源。
android:state_pressed
Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。
android:state_focused
Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。
android:state_selected
Boolean。“true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。
android:state_checkable
Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)
android:state_checked
Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。
android:state_enabled
Boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。
android:window_focused
Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。
注意:记住一点,StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。
Example:
XML文件保存在res/drawable/button.xml。
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/button_pressed"></item><!-- pressed --><item android:state_focused="true" android:drawable="@drawable/button_focused"></item><!-- focused --><item android:drawable="@drawable/button_normal"></item><!-- default --></selector>
Layout XML将这个Drawable应用到一个View上:
<imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/button"></imageview>
Color
定义在XML中的color,可以当作Drawable使用,来填充矩形区域(圆角可以有)。这种Drawable的行为很像是颜色填充。
注意:Color Drawable是一种简单的资源,可以使用name特性来引用其值(不再是XML文件的名)。因此,你可以在一个XML文件中的<resources>元素下添加多个Color Drawable。
File Location:
res/drawable/filename.xml
文件名随意。元素的name将会当作资源ID使用。
Complied Resource Datatype:
指向PaintDrawable资源的指针。
Resource Reference:
R.drawable.color_name(Java)
@[package:]drawable/color_name(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><resources><drawable name="color_name">color</drawable></resources>
Elements:
<resources>
必须。必须是根节点。
没有特性。
<drawable>
一个color Drawable。其值可以是任何有效的十六进制颜色值或者Color资源。Color值总是以“#”开头,后面紧跟Alpha-Red-Green-Blue信息,格式是:#RGB、#ARGB或者#AARRGGBB。
Attributes:
name
String。必须。Color的名字。这个名字将被当作资源ID使用。
Example:
XML文件保存在res/drawable/color.xml。
<?xml version="1.0" encoding="utf-8"?><resources><drawable name="solid_red">#f00</drawable><drawable name="solid_blue">#0000ff</drawable></resources>
Layout XML将会把这个Color Drawable应用到一个View上:
<textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/solid_blue"></textview>
代码中获取Color Drawable并应用到View上:
Resources res = getResources();
Drawable redDrawable = res.getDrawable(R.drawable.solid_red);
TextView tv = (TextView) findViewByID(R.id.text);
tv.setBackground(redDrawable);
Shape
定义在XML中的几何形状。
File Location:
res/drawable/filename.xml
文件名将被当作资源ID使用。
Complied Resource Datatype:
指向ShapeDrawable的资源指针。
Resource Reference:
R.drawable.filename(Java)
@[package:]drawable/filename(XML)
Syntax:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape='["rectangle"'><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"' android:useslevel='["true"'></gradient><solid android:color="color"></solid><stroke android:width="integer" android:color="color" android:dashwidth="integer" android:dashgap="integer"></stroke><padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer"></padding><corners android:radius="integer" android:topleftradius="integer" android:toprightradius="integer" android:bottomleftradius="integer" android:bottomrightradius="integer"></corners></shape>
Elements:
<shape>
必须。必须是根元素。
Attributes:
android:shape
Keyword。定义Shape的类型。有效的值包括:
Value
Desciption
"rectangle"
矩形。默认形状。
"oval"
椭圆。
"line"
水平直线。需要<stroke>元素定义线的宽度。
"ring"
环形。
接下来的特性只能在android:shape=”ring”时使用:
android:innerRadius
Dimension。内环的半径。
android:innerRadiusRatio
Float。以环的宽度比率来表示内环的半径。例如,如果android:innerRadiusRatio=”5”,内环半径等于环的宽度除以5。这个值可以被android:innerRadius覆盖。默认值是9。
android:thickness
Dimension。环的厚度。
android:thicknessRatio
Float。以环的宽度比率来表示环的厚度。例如,如果android:thicknessRatio=”2”,厚度就等于环的宽度除以2。这个值可以被android:thickness覆盖。默认值是3。
android:useLevel
Boolean。“true”表示可以当作LevelListDrawable使用。一般都为“false”。
<gradient>
为Shape指定渐变色。
Attributes:
android:angle
Integer。渐变色的角度值。0表示从左到右,90表示从下到上。必须是45的倍数,默认是0。
android:centerX
Float。渐变色中心的X相对位置(0-1.0)。当android:type=”linear”时无效。
android:centerY
Float。渐变色中心的Y相对位置(0-1.0)。当android:type=”linear”时无效。
android:centerColor
Color。可选的颜色,出现在start和end颜色之间。
android:endColor
Color。end颜色。
android:gradientRadius
Float。渐变色的半径。当android:type=”radial”时有效。
android:startColor
Color。start颜色。
android:type
Keyword。渐变色的样式。有效值为:
Value
Description
"linear"
线性渐变,默认值。
"radial"
环形渐变。start颜色是处于中间的颜色。
"sweep"
sweep渐变
android:useLevel
Boolean。“true”表示可以当作LevelListDrawable使用。
<solid>
填充shape的单一色。
Attributes:
android:color
Color。这个颜色会应用到shape上。
<stroke>
shape的线形。
Attributes:
android:width
Dimension。线的厚度。
android:color
Color。线的颜色。
android:dashGap
Dimension。间断线间的距离。仅在android:dashWidth设定时有效。
android:dashWidth
Dimension。间断线的大小。仅在android:dashGap设定时有效。
<padding>
内部View元素的边距。
Attributes:
android:left
Dimension。左内边距。
android:top
Dimension。上内边距。
android:right
Dimension。右内边距。
android:bottom
Dimension。下内边距。
<corners>
为shape创建圆角。当shape是一个矩形时有效。
Attributes:
android:radius
Dimension。圆角的半径。会被下面的特性覆盖。
android:topLeftRadius
Dimension。左上圆角半径。
android:topRightRadius
Dimension。右上圆角半径。
android:bottomLeftRadius
Dimension。左下圆角半径。
android:bottomRightRadius
Dimension。右下圆角半径。
Examples:
XML文件保存在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"></gradient><padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp"></padding><corners android:radius="8dp"></corners></shape>
Layout XML将被当作ShapeDrawable应用到一个View上:
<textview android:background="@drawable/gradient_box" android:layout_height="wrap_content" android:layout_width="wrap_content"></textview>
代码中也可以获得ShapeDrawable,然后应用到View上:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);</corners></padding></stroke></solid></gradient></stroke></shape></drawable></resources></resources></selector></item></item></selector></item></item></selector></linearlayout></stroke></text></id>
发表评论
-
TextView 的属性
2013-04-17 17:45 555收集到了TextView 的属性 ... -
ADT在线安装
2012-11-09 09:53 780注:转载自http://blog.csdn.net/kieve ... -
android 界面布局 很好的一篇总结 【转】
2012-04-26 15:24 916出处:http://www.cnblogs.com/awe ... -
android xml属性大全
2012-03-15 09:12 1011Android activity属性 android:all ... -
Android开发之屏幕大小自适应
2012-01-31 14:19 834屏幕大小: 一:不同的layout Android手机屏幕大小 ... -
Intent常用Uri
2012-01-21 09:37 801一、打开一个网页,类别是Intent.ACTION_VIEW ... -
android raw读取超过1M文件的方法
2011-11-01 15:48 821转载自:http://www.cnblogs.com/yaos ... -
Android Bitmap用法总结
2011-10-09 10:57 962转载自:http://blog.csdn.net/zhou69 ... -
Android之TextView------属性大全
2011-09-22 16:32 682android:autoLink设置是否当 ... -
Android内存泄漏简介
2011-09-09 16:28 804前言 不少人认为JAV ... -
Android的Parcel机制
2011-09-06 15:09 4016转载至:http://blog.csdn.net/caowen ... -
Android Context
2011-09-01 17:12 916在android中context可以作很多操作,但是最主要的功 ... -
android面试题
2011-06-27 11:57 9801.什么是Activity? 2.请描 ... -
android中的hdpi,ldpi,mdpi
2011-06-17 14:31 657Android2.1 和之后的版本 中的 drawable(h ... -
Android横竖屏
2011-06-08 11:22 853要解决的问题应该就两个: 一。布局问题; 二。重新载入问题。 ... -
访问android平台的通话记录CallLog
2011-06-01 15:31 1128转载自:http://android.tgbus.com/An ... -
Android软件权限知识普及
2011-05-27 11:08 857APK权限详细对照表 您的 ... -
Android调用WebService
2011-05-18 13:42 1059转载至:http://express.ruanko.com/r ... -
android实用代码片段
2011-05-06 10:58 7291. android获取到系统是24小时制还是12小时制 ... -
Android 应用程序退出的四种方法
2011-05-04 09:33 1464Android程序有很多Activi ...
相关推荐
主要介绍了android shape的使用及渐变色、分割线、边框、半透明阴影,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Android中使用Shape自定义形状
Android控件美化之Shape的使用 里面事例非常好的。
本文主要介绍Android Shape 控件的美化, 大家在开发Android程序的时候对系统自带的控件进行修改,这里给大家一个实例,供大家参考
shape实现控件圆角,背景,边框等属性
android-shape-imageview自定义形状的 imageview.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
【1】用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式; <?xml version=1.0 encoding=utf-8?><shape xmlns:android=http://schemas.android.com/apk/res/android> <gradient android:type=...
Android控件美化之Shape的使用[汇编].pdf
Android自定义矩形及selector、shape的使用Android自定义矩形及selector、shape的使用
本文主要记录Android控件设置阴影 给控件设置阴影,会使得界面元素更... 新建一个渐变的图层 item作为背景图层,主要是 gradient,shape是用来定义形状的,corners设置角度,gradient定义该形状里面为渐变色填充,star
android shape可以帮助我们实现很多的效果,比如:圆角、虚线边框、部分圆角、颜色的渐变等效果,同时源文件给出了android4.0系统下虚线会变成实线的解决办法。
android 用shape画出来的颜色做背景时,动态的改变它的颜色属性。
一个超轻量级(不到400行代码),功能强大的自定义Imageview控件,支持长方形,正方形,圆角矩形,圆形,三角形,棱形,任意正多边形,边框,边角装饰。
android-shape-imageview - 图片圆角、三角、五角、圆形、多角
android shape的使用
本文实例讲述了Android编程使用自定义shape实现shadow阴影效果的方法。分享给大家供大家参考,具体如下: 直接上xml文件, 并且附上相应的解析: <?xml version=1.0 encoding=utf-8?> <selector xmlns:android...
梯形控件,the Trapezoid Parts View which draw Trapezoid image. and event on it.
Android中常常使用shape来定义控件的一些显示属性,接下来通过本篇文章给大家介绍Android中shape定义控件的使用,对android shape定义控件相关知识感兴趣的朋友一起学习吧
Android shape属性详解