当系统提供的控件,无法满足我们的需求的时候,我们往往会 想到自定义控件,通过继承View来实现。当我们想像 系统提供的控件那样可以如android:text="hello",这样设置自定义控件中的文字显示去,这时我们该怎么办?
带着问题,我们会想到在继承View的构造函数中 有个AttributeSet这个属性,没错 这就是 突破口。
我们可以在attrs.xml中声明自己控件的属性,在布局xml文档中声明自己的命名空间,这时就可以对设置自己想要的值了,然后在AttributeSet这个属性中获取对应的值。好了不多说,我们来看下代码,一切尽在不言中:
工程下载地址:点击打开链接
第一步:
在/res/values下编写一个attrs.xml的文件,其中内容结构如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="my_view">
<attr name="text_color" format="color"></attr>
<attr name="bg_color" format="color"></attr>
<attr name="text" format="string"></attr>
</declare-styleable>
</resources>
其中要说的是format 格式,对于它的格式说明将在文章后面介绍。
第二步:
写一个自定义的Veiw,如下代码:
public class myView extends View{
private Paint paint;
private int bg_color,text_color,alpha;
private String text;
public myView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
TypedArray s = context.obtainStyledAttributes(attrs, R.styleable.my_view);//从xml那传来的一组值
bg_color = s.getColor(R.styleable.my_view_bg_color, R.color.transparent);//得到attrs中对应属性的bg_color值
text_color = s.getColor(R.styleable.my_view_text_color, android.R.color.black);//..
text = s.getText(R.styleable.my_view_text).toString();//..
}
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
super.draw(canvas);
paint.setColor(bg_color);
Rect r = new Rect(1, 80, 60, 140);
canvas.drawRect(r, paint);
paint.setColor(text_color);
canvas.drawText(text, 10, 100, paint);
}
}
注释在重点地方已经写清楚了,也就值AttributeSet attrs 的使用,通过它可以获取在XML中定义的值。
第三步:
在布局文档中声明你的View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.defineview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 先声明命名空间 res/包名 以下就可以通过app:+attrs定义的属性,进行值设置 -->
<com.example.defineview.myView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:text="nihao"
app:text_color="#ff0000"
app:bg_color="#00ff00"
/>
</LinearLayout>
其中
xmlns:app="http://schemas.android.com/apk/res/com.example.defineview"
是自己声明的命名空间,app是头自由命名,在res/+自己的包名
这时你就可以使用
app:text="nihao"
这样的属性了,注意text、bg_color这些属性是在attrs中设置好的。
好了这样就OK了我们来看下运行效果:
运行效果中是不是显示出了
app:text="nihao"
app:text_color="#ff0000"
app:bg_color="#00ff00"
的效果。
我们看下文件的目录结构
附录:attrs
在这里,需要补充attrs属性的相关知识,即Attr属性是如何在XML中定义的,自定义属性的Value值可以有10种类型以及其类型的组合值,其具体使用方法如下:
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使用AttributeSet自定义控件的方法
-
android获取mp4视频文件总时长和视频宽高,MediaMetadataRetriever类、方法以及使用详解
根据该方式获取视频信息可以看出不仅仅可以获取时长和分辨率,还能获取到其他的一些视频信息,不错还是很强大的,不用自己去解析mp4文件了。此demo只是获取的mp4文件,其他的多媒体文件也可以获取到相应信息,比如mp3;
-
spring boot 在Linux下服务启动报错Unable to find Java
将 Spring boot 安装为 Linux 服务启动,后输入 service myapp start 报错 Unable to find Java ,但是使用 java -jar myapp.jar 启动成功。不知道为啥引起的,经过百度找到下面这个解决方法和我的情况一样,终于把问题解决
-
三种搭建npm仓库的方法
Git仓库当私有npm
-
npm的.npmrc文件在哪里?缓存及全局包文件在什么位置?
就是那些通过npm install xxx -g或者cnpm install xxx -g或者yarn global add xxx安装的xxx文件,到底安装在什么地方?这个其实也挺好找的。默认情况下,可以通过下面的命令查看其基础路径。
-
根据条件配置多个npm仓库
scope 是一种很好的包管理方式。统一的“命名空间”,清晰、好辨识;在 registry 中使用统一的 organization 管理,不必担心命名冲突和冒用等。 在实际使用中,一个常见的场景是公司的私有仓库。使用统一的 scope 定义在私有仓库中定义私有包,绝对是一个非常好的方式。 指定 scope 从指定仓库安装
-
verdaccio 搭建 npm 服务器以及发布流程
搭建私服好处多多哦
-
百度地图自定义图层----BMapTileCutterJava切图工具网络版
这篇文章实现了这个切图的功能,但是最新的百度 最高缩放等级达到19级,项目需要,无奈只能自己写个切图开发工具。 参考了同样作者的另一篇文章
-
瓦片地图生成使用以及原理
我们都知道地球是圆的,电脑显示器是平的,要想让位于球面的形状显示在平面的显示器上就必然需要一个转换过程,这个过程就叫做投影(Projection)。在地球上我们通过经纬度来描述某个位置,而经过投影之后的地图也有自己的坐标系统,本篇文章就来详细介绍在百度地图API中涉及的各种坐标体系。
-
腾讯地图谷歌和高德地图等自定义地图区别
腾讯、百度、Google的地图投影均采用Web Mercator 投影坐标系;腾讯与Google的地图瓦片分辨率及切片范围是完全相同的,仅仅是命名规则稍有不同,这就使得同一位置和缩放级别的地图瓦片是完全可以重叠的;而百度地图每个缩放级别分辨率与前两者均不相同,而且地图瓦片的坐标原点做了一定的偏移,导致百度地图与前两者的瓦片是无法重叠的,这是因为百度在GCJ-02的基础上又进行了加密处理,形成了百度独有的BD-09坐标系。
-
v4jar包keyeventcompat不存在的错误
KeyEventCompat类被取消了 hasNoModifiers方法已经被KeyEvent实现了