最近在做软件从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 文件.代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="MyView">
  4. <attr name="textColor" format="color" />
  5. <attr name="textSize" format="dimension" />
  6. </declare-styleable>
  7. </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() 方法,为了保持以后使用该属性一致性!

  1. public MyView(Context context,AttributeSet attrs)
  2. {
  3. super(context,attrs);
  4. mPaint = new Paint();
  5. TypedArray a = context.obtainStyledAttributes(attrs,
  6. R.styleable.MyView);
  7. int textColor = a.getColor(R.styleable.MyView_textColor,
  8. 0XFFFFFFFF);
  9. float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
  10. mPaint.setTextSize(textSize);
  11. mPaint.setColor(textColor);
  12. a.recycle();
  13. }

MyView.java  MyView控件全部代码如下:

  1. package com.android.tutor;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Rect;
  8. import android.graphics.Paint.Style;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11. public class MyView extends View {
  12. private Paint mPaint;
  13. private Context mContext;
  14. private static final String mString = "Welcome to Mr Wei's blog";
  15. public MyView(Context context) {
  16. super(context);
  17. mPaint = new Paint();
  18. }
  19. public MyView(Context context,AttributeSet attrs)
  20. {
  21. super(context,attrs);
  22. mPaint = new Paint();
  23. TypedArray a = context.obtainStyledAttributes(attrs,
  24. R.styleable.MyView);
  25. int textColor = a.getColor(R.styleable.MyView_textColor,
  26. 0XFFFFFFFF);
  27. float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
  28. mPaint.setTextSize(textSize);
  29. mPaint.setColor(textColor);
  30. a.recycle();
  31. }
  32. @Override
  33. protected void onDraw(Canvas canvas) {
  34. // TODO Auto-generated method stub
  35. super.onDraw(canvas);
  36. //设置填充
  37. mPaint.setStyle(Style.FILL);
  38. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  39. canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
  40. mPaint.setColor(Color.BLUE);
  41. //绘制文字
  42. canvas.drawText(mString, 10, 110, mPaint);
  43. }
  44. }

三、将我们自定义的MyView 加入布局main.xml 文件中,并且使用自定义属性,自定义属性必须加上:

xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor"  ,test是自定义属性的前缀,           com.android.tutor 是我们包名.

main.xml 全部代码如下:

  1. <?xml
  2. version="1.0" encoding="utf-8"?>
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
  6. android:orientation="vertical"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. >
  10. <TextView
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello"
  14. />
  15. <com.android.tutor.MyView
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. test:textSize="20px"
  19. test:textColor="#fff"
  20. />
  21. </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>

from:http://jiayanjujyj.iteye.com/blog/1392541

【转】说说Android中的style和theme的更多相关文章

  1. Android 中的style和Theme的使用

    说明 style和theme的定义是为了改变原有系统设定的默认窗体.字体.背景色.格式等风格而使用.其本质就是系统属性的集合.本篇主要介绍android中的style和theme的具体用法. styl ...

  2. Android中的 style 和 theme

    通过设置 view 控件的属性,达到设置android UI的目的,如果某些 属性值复用率很高,可以考虑将属性单独声明在 style中,这样就可以达到复用的效果. 一.style Style 概念:A ...

  3. 【转】android中的Style与Theme

    Android默认情况下提供了一些实用的主题样式,比如说Theme.Dialog可以让你的Activity变成一个窗口风格,而Theme.Light则让你的整个Activity具有白色的背景,而不是黑 ...

  4. android中的style部分属性值介绍

    转自:http://blog.sina.com.cn/s/blog_70c759fd01013phv.html Android平台定义的主题样式: android:theme="@andro ...

  5. android中的style部分属性值介绍 --zz

    Android平台定义的主题样式: android:theme="@android:style/Theme.Dialog"   将一个Activity显示为对话框模式 •andro ...

  6. ArcGIS Runtime SDK for Android中SimpleFillSymbol.Style样式

    SimpleFillSymbol.Style样式枚举共8种: 1.BACKWARD_DIAGONAL 反对角线填充 2.CROSS 交叉线填充 3.DIAGONAL_CROSS 前后对角线填充 4.F ...

  7. Android中Style和Theme的使用

    Style: Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色, ...

  8. Android中theme.xml与style.xml的区别

    一.相同点 两者的定义相同.继承方式也相同 <?xml version="1.0" encoding="utf-8"?> <resources ...

  9. Android中style和theme的区别

    在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout .RelativeLayout.TableLayout.FrameLayou ...

随机推荐

  1. ios app真正的相互!!调用

    1.需求:A应用打开B.B回跳到A   2.问题: 看到网络上的文档讲的大多数都是app单向跳转的例子,而我们在跳转到第二个app的时候往往需要返回到原来的app,虽然支付宝微信等第三方等应用会有回调 ...

  2. 【转】Java 异步处理简单实践

    同步与异步 通常同步意味着一个任务的某个处理过程会对多个线程在用串行化处理,而异步则意味着某个处理过程可以允许多个线程同时处理. 异步通常代表着更好的性能,因为它很大程度上依赖于缓冲,是典型的使用空间 ...

  3. SpringBoot(五) Web Applications: MVC

    统一异常处理 SpringBoot的默认映射 /error 码云: commit: 统一异常处理+返回JSON格式+favicon.ico 文档: 28.1.11 Error Handling 参考 ...

  4. js-权威指南学习笔记18

    1.除mouseenter和mouseleave外的所有鼠标事件都能冒泡. 2.传递给鼠标事件处理程序的事件对象有clientX和clientY属性,它们制订了鼠标指针相对于包含窗口的坐标. 3.一个 ...

  5. drupal7 模糊查询接口

    $query->condition('card_no', db_like($batch_no).'%', 'LIKE');

  6. JavaScript--浅谈DOM操作

    JavaScript之浅谈DOM操作 1.理解DOM: DOM(Document Object Model ,文档对象模型)一种独立于语言,用于操作xml,html文档的应用编程接口. 怎么说,我从两 ...

  7. linux 挂载命令mount、umount

    mount /bin/mount语法:mount [-t文件系统] 设备文件名 挂载点mount -t iso9660 /dev/sr0 /mnt/cdromiso9660是固定的,光盘:所以 -t ...

  8. leetCode题解 Reverse Words in a String III

    1.题目描述 Given a string, you need to reverse the order of characters in each word within a sentence wh ...

  9. Oracle EBS 应收发票取值

    SELECT ct.trx_number ,ctl.description ,fnd_flex_ext.get_segs('SQLGL' ,'GL#' ,gcc.chart_of_accounts_i ...

  10. Oracle EBS 查询客户报错 查询已超出 200 行。可能存在更多的行,请限制查询。