- 浏览: 444197 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
supertangcugu:
文中有个错误,SoftReference是软引用,Strong ...
WeakReference与SoftReference -
somefuture:
我每次走到movie.draw(canvas,0, 0); ...
利用Movie播放GIF -
鞠文婷:
徐颖慧 写道为什么打不开啊?额 果然打不开了。。
50个Android开发人员必备UI效果源码[转载] -
徐颖慧:
为什么打不开啊?
50个Android开发人员必备UI效果源码[转载] -
李白625:
为什么打不开啊
50个Android开发人员必备UI效果源码[转载]
转自:http://jiayanjujyj.iteye.com/blog/1392541
最近在做软件从2.3到4.0的改变的一些工作,其中涉及了一些style和theme相关的东西。上网上查了一些东西,这个一并说说。关于android中style和theme的基本使用,这里就不再赘述了,可以查看Dev Guide上的东东,这里主要说说自己比较困惑的一些部分。
Android platform已经提供了许多的style和theme供开发者使用,可以在R.style类中找到可供使用的style,不过需要把其中的下划线(_)改成点号(.). 如果我们查看R.style类的文档,发现有些style没有描述或者描述的不怎么清楚,还是看看原文件中怎么定义的吧。style和themem的原文件可以在<ANDROID_SDK_HOME>/platforms/android-15/data/res/values/下面找到,其中styles.xml和theme.xml文件就是R.style类的定义文件。可以看到theme.xml中定义了Theme.Holo, Theme.Holo.Light等theme。
在values文件下还有一个文件就是attrs.xml,这是R.attr和R.styleable类的定义文件。attrs.xml中定义了每个view的可用的属性,例如使用android:textAppearance就是在attrs.xml中定义了<attr name="textAppearance" format="reference" />,那麽这里点format="reference"使什么意思呢?这里解释一下,一些例子来源于网络:
1. reference:参考某一资源ID。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>
2. color:颜色值
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
3. boolean:布尔值
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
4. dimension:尺寸值。
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
5. float:浮点值。
6. integer:整型值。
7. string:字符串
8. fraction:百分数。
9. enum:枚举值
10. flag:位或运算
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID|#00FF00"
/>
ok, 了解完这些,下面说说怎么自定义View和属性,可以参考http://blog.csdn.net/jincf2011/article/details/6344678
在xml 文件里定义控件的属性,我们已经习惯了android:attrs="" ,那么我们能不能定义自己的属性能,比如:test:attrs="" 呢?答案是肯定的.
进入主题。大致以下步骤:
一、 在res/values 文件下定义一个attrs.xml 文件.代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="MyView">
- <attr name="textColor" format="color" />
- <attr name="textSize" format="dimension" />
- </declare-styleable>
- </resources>
二、 我们在MyView.java 代码编写如下,其中下面的构造方法是重点,我们获取定义的属性R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!
MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!
- public MyView(Context context,AttributeSet attrs)
- {
- super(context,attrs);
- mPaint = new Paint();
- TypedArray a = context.obtainStyledAttributes(attrs,
- R.styleable.MyView);
- int textColor = a.getColor(R.styleable.MyView_textColor,
- 0XFFFFFFFF);
- float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
- mPaint.setTextSize(textSize);
- mPaint.setColor(textColor);
- a.recycle();
- }
MyView.java MyView控件全部代码如下:
- package com.android.tutor;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.graphics.Paint.Style;
- import android.util.AttributeSet;
- import android.view.View;
- public class MyView extends View {
- private Paint mPaint;
- private Context mContext;
- private static final String mString = "Welcome to Mr Wei's blog";
- public MyView(Context context) {
- super(context);
- mPaint = new Paint();
- }
- public MyView(Context context,AttributeSet attrs)
- {
- super(context,attrs);
- mPaint = new Paint();
- TypedArray a = context.obtainStyledAttributes(attrs,
- R.styleable.MyView);
- int textColor = a.getColor(R.styleable.MyView_textColor,
- 0XFFFFFFFF);
- float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
- mPaint.setTextSize(textSize);
- mPaint.setColor(textColor);
- a.recycle();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- //设置填充
- mPaint.setStyle(Style.FILL);
- //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
- canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
- mPaint.setColor(Color.BLUE);
- //绘制文字
- canvas.drawText(mString, 10, 110, mPaint);
- }
- }
三、将我们自定义的MyView 加入布局main.xml 文件中,并且使用自定义属性,自定义属性必须加上:
" xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor" ,test是自定义属性的前缀, com.android.tutor 是我们包名.
main.xml 全部代码如下:
- <?xml
- version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <com.android.tutor.MyView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- test:textSize="20px"
- test:textColor="#fff"
- />
- </LinearLayout>
四、运行之效果如下图:
最后:
关于在引用资源时使用@还是?的问题,我们在设置style的时候既可以使用@也可以使用?, 例如android:textAppearance="@andorid:style/TextAppearance.Medium",
android:textAppearance="?android:attr/textAppearanceMedium"
使用@表示使用固定的style,而不会跟随Theme改变,这个style可以在style.xml中找到。
而?表示从Theme中查找引用的资源名,例如上面的textAppearanceMedium,查看themes.xml文件,可以看到在不同的theme中,textAppearanceMedium引用的style是不同的。如在Them.Holo中<item name="textAppearanceMedium">@android:style/TextAppearance.Holo.Medium</item>
,Theme.Holo.Light中为<item name="textAppearanceMedium">@android:style/TextAppearance.Holo.Light.Medium</item>
发表评论
-
Android OpenGL库函数列表
2014-11-26 10:44 1204Android OpenGL库函数列表 OpenGl核 ... -
FFmpeg Android部分的编译
2014-06-19 17:09 1189编译的是最新版2.2.3。再说编译环境 操作系统 ubun ... -
AVPacket与AVFrame的关系
2014-06-19 16:19 9967最近在做ffmpeg相关的项目,满眼的AVPacket和AV ... -
WebView 自适应全屏
2013-10-30 11:29 5228网上有通行的3中方法 写道 第一种: settings ... -
NDK调试
2013-10-24 16:41 5834大体根据官方攻略 http://tools.android ... -
adb logcat 打印输出
2013-08-06 11:43 3116深圳公司的同事在测试的时候遇到一些bug 我这边没办法重 ... -
android上加载大图片需要注意的问题
2013-07-16 21:47 9391尽量不要使用setImageBitmap或setImageR ... -
XMPP下的注册与登录
2013-06-24 23:43 6696前面讲了本地如何搭建XMPP环境 其实我不懂 也没认真的好好 ... -
XMPP 环境快速搭建
2013-06-24 22:31 1222这年头微信火 于是好多公司都要跟风 搭建环境其实挺简单 ... -
利用Movie播放GIF
2013-05-09 16:52 5095public class ViewContent ex ... -
ListView分隔线的显示
2013-04-08 16:21 1539项目中需要在listview中添加footview 出现了 ... -
结合popwindow与gridview的一例
2013-03-15 16:04 1379项目中需要实现一个gridview 点击某项弹出一个popu ... -
[转]Android中Matrix的pre post set方法理解
2013-03-08 15:03 1229虽说以前学习过线性代数和图形学原理,但是在实际中碰到matr ... -
50个Android开发人员必备UI效果源码[转载]
2013-03-08 09:29 44534Android 仿微信之主页面实现篇Android 仿微信之界 ... -
对View DrawingCache的理解
2013-02-18 19:37 1079View组件显示的内容可以通过cache机制保存为bitm ... -
Fragment使用方法一则
2013-02-18 17:28 1533之前在项目里也用到了fragment,作为横向滑动的组件显示 ... -
AVD ROOT
2012-12-04 11:47 3188启动一个模拟器,开 ... -
onTouchEvent 、onInterceptTouchEvent的顺序~
2012-10-26 10:29 1170转自:http://yxwww.iteye.com/blog/ ... -
ListView滑动获取分页 并异步加载每一项的图片
2012-09-26 10:40 4488项目有一个需求就是从服务器获取一个分页数据,数据按json数组 ... -
解析apk文件的图标等内容
2012-09-05 15:56 3260import java.lang.reflect. ...
相关推荐
在values中定义一个attrs.xml 然后添加相关属性 这一篇先详细介绍一下attrs.xml的属性。 <?xml version=1.0 encoding=utf-8?> //自定义属性名,定义公共属性 <attr name=titleText format=string/> ...
大家也可以结合《理解Android中的自定义属性》这篇文章进行学习,后续一篇还有应用。 1、attrs文件编写 <?xml version=1.0 encoding=utf-8?> <resources> <attr name=titleText format=string> ...
Attrs 属性 attr format description duration integer 动画持续时间 stroke_width dimension 未选中时边框宽度 color_tick color 对勾颜色 color_checked color 选中时填充颜色 color_unchecked ...
SplitLayout Android SplitLayout, which splits the available space between two child views by dragging the center handle. ...Attrs 属性 attr format description default splitOrientatio
fix-attrs fix --format yaml file.cfg 它根据文件扩展名检测格式: fix-attrs fix file.yml 编译兼容版本: OS=(linux darwin) for i in "${OS[@]}"; do CGO_ENABLED=0 GOOS=$i GOARCH=amd64 \ go build \ ...
在assets文件夹下放images+json文件就可以动态生成gif的android lottie jar包,需要在自己工程里面的values/attrs.xml加上自定义属性 <attr name="lottie_fileName" format="string" /> ...
attrs.xml 属性: <!--gif文件引用--> <attr name="gif_src" format="reference" /> <!--是否加载完自动播放--> <attr name="auth_play" format="boolean" /> <!--播放次放,默认永远播放--> ...
最近接触了Android自定义控件,涉及到自定义xml中得属性(attribute),其实也很简单,但是写着写着,发现代码不完美了,就是在attrs.xml这个文件中,发现属性冗余,于是就想有没有类似属性继承或者include之类的方法....
第一步:在res\values的目录下新建一个文件attrs.xml 声明一些自定义属性 <?xml version=1.0 encoding=utf-8?> <attr name=customText format=string> <attr name=customTextColor format=c
首先创建values\attrs.xml,在attrs.xml中声明自定义属性: 自定义string类型,属性名为text 自定义color类型,属性名为textColor 自定义dimension类型,属性名为textSize declare-styleable这个标签的作用其实...
自绘控件的内容都是自己绘制出来的 大致...2.根据需求在attrs文件中自定义属性 declare-styleable 声明自定义属性可以自定义一个新属性也可以引用已经存在的属性两者的区别就是新属性需要添加format进行类型的定义 3
首先我们在attrs属性文件中增加几个自定义属性 <?xml version=1.0 encoding=utf-8?> <!-- 圆形进度条进度显示的颜色 --> <attr name=roundProgressColor format=color> <!-- 外圈圆的颜色 --&...
在attrs.xml 中增加自定义的属性 <!-- 飘心动画自定义的属性 --> <attr name=initX format=dimension/> <attr name=initY format=dimension/> <attr name=xRand format=dimension/> <attr name=...
xml中设置自定义属性 分三步: 1. 在项目中的values文件中创建attrs文件 <?xml version=1.0 encoding=utf-8?> <resources> <declare name=QLoadingIndicatorView> <attr name=indicatorId format=integer...
#MaterialRangeSlider 材料设计滑块,用于选择一个值范围。... 您还需要在attrs.xml中声明自定义属性。 attrs.xml <? xml version = " 1.0 " encoding = " utf-8 " ?> < attr name = " max " format
#MaterialRangeSlider 材料设计滑块,用于选择一个值范围。... 您还需要在attrs.xml中声明自定义属性。 attrs.xml <? xml version = " 1.0 " encoding = " utf-8 " ?> < attr name = " max " format
2、在View的构造方法中获得我们自定义的属性 [3、重写onMeasure] 4、重写onDraw 1.首先在我们的res/values/目录下建立一个attrs.xml文件,然后在里面声明我们我们需要的自定义属性 我们定义了矩形的颜色,矩形的...
一、创建属性文件(attrs.xml) 具体操作: 1、在项目的values文件底下创建一新的属性文件,文件名为attrs:New->XML->Values XML File: 2、补充attrs.xml代码: <?xml version=1.0 encoding=utf-8?> ...
第一步:创建attrs文件夹,自定义属性: <?xml version=1.0 encoding=utf-8?> <attr name=out_color format=color/> <attr name=inner_color format=color/> <attr name=border_width format=d
在上一篇的文章中介绍了自定义控件的属性,详情见《详解Android自定义控件属性TypedArray以及attrs》。那么在这基础上实现随机验证码生成,里面的代码是自定义控件以及涉及到自定义view绘画。 1、先看实现的效果图 ...