$*********************************************************************************************$


博主推荐:


风萧兮兮易水寒,“天真”一去兮不复还。如何找到天真的那份快乐。小编倾力推荐app: 天真无谐


下载方式:豌豆荚,应用宝,360手机助手,百度手机助手,安卓,91市场搜索:天真无谐

关注我们:查看详情

$*********************************************************************************************$

Android中在values中定义一个attrs.xml,然后自己定义一个组件MyView

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:

package com.jiangwei.demo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View; public class MyView extends View {
private Paint mPaint;
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);
}
}

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.jiangwei.demo"//一定记得添加前缀
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.jiangwei.demo.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"//test是个前缀
test:textColor="#ffffff"/>
</LinearLayout>

具体内容:

格式如上,其中“xmlns:wen”冒号后面是标签名,在下面使用时(只对当前文件可用)
<TextView  wen:属性名/>
“com.iteye.googlers”是你的工程包名。
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:颜色值。
    (1)属性定义:

<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>

(2)属性使用:
           

 <TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>

3. boolean:布尔值。
    (1)属性定义:
            

<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>

(2)属性使用:
            

<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"/>

4. dimension:尺寸值。
     (1)属性定义:
             

<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>

(2)属性使用:
            

<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>

5. float:浮点值。
    (1)属性定义:
            

<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>

(2)属性使用:
            

<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>

6. integer:整型值。
    (1)属性定义:
            

<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>

(2)属性使用:
            

<animated-rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:drawable = "@drawable/图片ID"
android:pivotX = "50%"
android:pivotY = "50%"
android:framesCount = "12"
android:frameDuration = "100"
/>

7. string:字符串。
    (1)属性定义:
            

<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>

(2)属性使用:
            

<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>

8. fraction:百分数。
     (1)属性定义:
            

<declare-styleable name="RotateDrawable">
<attr name = "visible" />
<attr name = "fromDegrees" format = "float" />
<attr name = "toDegrees" format = "float" />
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
<attr name = "drawable" />
</declare-styleable>

(2)属性使用:
            

<rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
   android:interpolator = "@anim/动画ID"
android:fromDegrees = "0"
   android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
   android:duration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"
/>

9. enum:枚举值。
    (1)属性定义:
            

<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>

(2)属性使用:

<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>

10. flag:位或运算。
     (1)属性定义:

 <declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>

(2)属性使用:
            

<activity
android:name = ".StyleAndThemeActivity"
android:label = "@string/app_name"
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

注意:
     属性定义时可以指定多种类型值。
    (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"
/>

Android中attrs.xml文件的使用详解的更多相关文章

  1. 【转】Mybatis 3.1中 Mapper XML 文件 的学习详解

    MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 ...

  2. Mybatis 3.1中 Mapper XML 文件 的学习详解(转载)

    MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 ...

  3. 修改Android中strings.xml文件, 动态改变数据

    有些朋友可能会动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法.strings.xml中节点是支持占位符的,如下所示: <string name=&qu ...

  4. Android 中各种权限深入体验及详解

    Android 中各种权限深入体验及详解 分类: Android2012-07-15 19:27 2822人阅读 评论(0) 收藏 举报 androidpermissionsinstallersyst ...

  5. Python中__init__.py文件的作用详解

    转自http://www.jb51.net/article/92863.htm Python中__init__.py文件的作用详解 http://www.jb51.net/article/86580. ...

  6. php中读取大文件实现方法详解

    php中读取大文件实现方法详解 来源:   时间:2013-09-05 19:27:01   阅读数:6186 分享到:0 [导读] 本文章来给各位同学介绍php中读取大文件实现方法详解吧,有需要了解 ...

  7. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  8. Android中fragment_main.xml文件里的组件获取的问题

    package com.dhy.phonedial; import android.app.Activity; import android.app.Fragment; import android. ...

  9. Android之旅十四 android中的xml文件解析

    在我们做有关android项目的时候,肯定会涉及到对xml文件的解析操作.以下给大家介绍一下xml文件的解析.包括DOM.SAX.Pull以及曾经我们用到的DOM4J和JDOM: 要解析的XML文件: ...

随机推荐

  1. MailUtils

    /** *包名:com.thinkgem.jeesite.test *描述:package com.thinkgem.jeesite.test; */ package com.thinkgem.jee ...

  2. tomcat的stratup小黑框名字修改

    Tomcat的bin目录下:catalina.out文件, TITLE就是Tomcat startup窗口的名称,282行默认是:Tomcat.这里建议改成“服务名+端口号”的方式,看起来一目了然.因 ...

  3. Vue使用lib-flexible,将px转化为rem

    1.下载lib-flexible 我使用的是vue-cli+webpack,所以是通过npm来安装的 npm i lib-flexible --save 2.引入lib-flexible 在main. ...

  4. 【Java架构:基础技术】一篇文章搞掂:Idea

    一.使用技巧 1.1.配置Maven 打开File-Settings打开设置界面 1.2.配置JDK JDK可以设置默认版本,也可以设置针对某个项目 分别对应File-Other Setting-De ...

  5. 2019ccpc秦皇岛/Gym102361 I - Invoker dp

    题意: 连续3个特定的按键(在这3个中不要求顺序)能使出某个技能,使出不同技能所需要的按键可以重叠,给你一个技能序列,问你最少花费多少次按键能按顺序使出这些招数. 题解: dp,dp[i][j]代表使 ...

  6. 过滤PostgreSQL配置文件中被注释的部分

    以下正则可以过滤掉PostgreSQL配置文件被注释的部分,包括'#'前带空格的部分,但参数前带空格的部分不会过滤掉 postgres@linux-ij7j:/opt/pg8122/data> ...

  7. (54) C# 调用 kernel32.dll

    https://www.cnblogs.com/cwy173/archive/2010/10/02/1841321.html Kernel32 API AddAtom 向本地原子表添加一个字符串 Al ...

  8. Excel表格文本格式的数字和数字格式如何批量转换

    Excel表格文本格式的数字和数字格式如何批量转换 在使用Excel表格对数据求和时,只能对单元格内常规格式的数据进行计算,而不能对单元格中的文本格式的数据进行计算,特点就是在单元格的左上角有一个绿色 ...

  9. Hexo next 添加复制粘贴代码的功能

    文章目录 广告: 自己的方式 感谢 广告: 本人博客地址:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io ...

  10. 通过angular.js实现MVC的基本步骤

    通过ng实现MVC的基本步骤: ①创建模块 var app = angular.module('模块名字',['依赖模块1','依赖模块2']) ②调用模块 ngApp--> ng-app=&q ...