当系统提供的控件,无法满足我们的需求的时候,我们往往会 想到自定义控件,通过继承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自定义控件的方法
-
Error: tunneling socket could not be established, cause=connect ECO微信小程序本地调试预览出错
找到开发工具中 “工具 - 设置 - 代理设置”,选择不使用任何代理即可
-
IDEA从mapper.java跳转到mapper.xml
在IDEA中写项目后台的时候,从controller到service到mapper到dao,都可以直接跳转,但是mapper.java到mapper.xml就需要自行寻找,为了开发方便,安装相应插件--mybais
-
git pre-commit hook failed 解决办法
今天在上传项目的时候在commit阶段遇到一个问题,无论是在Sourcetree上传还是用命令git commit -m 'xxx'都报了一下错误:
-
git index.lock
因是在你进行某些比较费时的git操作时自动生成,操作结束后自动删除,相当于一个锁定文件,目的在于防止对一个目录同时进行多个操作。 有时强制关闭进行中的git操作,这个文件没有被自动删除,之后你就无法进行其他操作,必须手动删除,进入.git文件中删除,打开显示隐藏文件。如果没有看见.git文件夹,可以直接用命令rm -f ./.git/index.lock。之后就可以正常使用。 ———————————————— 版权声明:本文为CSDN博主「李瑞豪」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_39520417/article/details/81941111
-
IntelliJ IDEA 进阶教程: 语言注入
我真的太久没发 IntelliJ 的教程了,最近 Sakura 同学找我帮他配 IntelliJ ,我秀了一下技术,假装自己是老司机。 然后发现 Language Injection 这个非常好用的功能我居然没写教程(很明显,一堆人不知道这个东西), 于是我又开始写教程了。 本文多图。
-
SpringBoot+Mybatis,返回Map的时候,将Map内的Key转换为驼峰的命名表达式
用Map接收的时候,都是像DB定义的字段一样,类似以下 student_name,student_id,没有转换为驼峰,但是又不能因为这一个定义一个javabean来映射数据库字段集合,这样,会有无穷无尽的javabean,完全不是办法,
-
SrpingBoot 热启动与热部署
当我们已经启动了一个服务,然后修改代码之后,会自动重新部署。Spring项目通常有新内容修改后需要重新编译然后运行。通过配置Spring boot的热启动配置,可以实现自动编译重启项目,通常要比手动停止,启动项目快。
-
nginx的proxy_pass路径转发规则浅析(末尾/问题)
location匹配路径末尾没有 /,proxy_pass后面的路径只有域名同时最后有 /:
-
WIN系统如何通过快捷键启动桌面应用(或软件,或bat)
通过快捷键直接启动电脑上的相关应用,可以分成两个步骤: 1.相关启动应用生成快捷方式(如果已经生成了快捷键,可以跳过这步); 2.为快捷方式设置相应的快捷键。
-
WARNING: API 'variantOutput.getProcessResources()' is
解决办法,更新bugly版本