android 自定义控件库 属性怎么用

\ Android自定义控件学习流程
Android自定义控件学习流程
移动开发工程师
不知天上宫阙今夕是何年!
作者的热门手记
简单梳理了一下自定义控件的初级到高级的流程,还有很多没有细化,后面慢慢更新,当年总结这个是为了做教案的。
=======================================================
控件的作用?
那些地方使用了控件?
为什么要封装为控件?
第一个自定义控件
为什么要自定义控件?
自定义控件如何使用?
自定义控件更改方法:
onSizeChanged
自定义控件之画笔
自定义控件之画布
自定义控件之图层
自定义控件之Path
自定义控件之文字
自定义控件之叠加
自定义控件之阴影
自定义控件之动画
什么是属性动画?
属性动画能做的事儿
动画之时间差值
动画之合成
如何高效使用动画
自定义控件之属性
属性的定义
属性值获取
属性的默认值
属性与Style直接的关系
自定义控件之背景
Drawable初识
Drawable的作用
Drawable中进行复杂操作
自定义控件之Drawable动画
动画的定义
动画的启动条件
动画与时间插值结合
自定义控件之分类
实战控件之MD Button
实战控件之MD Action Button
实战控件之MD 涟漪界面
相关标签:
请登录后,发表评论
评论(Enter+Ctrl)
评论加载中...
评论加载中...
Copyright (C)
All Rights Reserved | 京ICP备 号-215:05 提问
自定义控件引用不到自定义的属性值
&?xml version="1.0" encoding="utf-8"?&
&resources&
&declare-styleable name="Bottom"&
&attr name="oneText" format="string"/&
&attr name="twoText" format="string"/&
&attr name="threeText" format="string"/&
&attr name="oneTextSize" format="dimension"/&
&attr name="twoTextSize" format="dimension"/&
&attr name="ThreeTextSize" format="dimension"/&
&attr name="oneTextColor" format="color"/&
&attr name="twoTextColor" format="color"/&
&attr name="ThreeTextColor" format="color"/&
&attr name="oneImageIcon" format="reference|color"/&
&attr name="twoImageIcon" format="reference|color"/&
&attr name="threeImageIcon" format="reference|color"/&
&attr name="backColor" format="color"/&
&/declare-styleable&
&/resources&
&com.example.myappzdybottom.zdy.MyBottom
android:layout_width="fill_parent"
android:layout_height="200dp"
mybottom:oneText="第一个"
mybottom:twoText="第二个"
mybottom:oneTextSize="8sp"
mybottom:oneTextColor="#00FFFF"
mybottom:twoTextColor="#FF0000"
&&/com.example.myappzdybottom.zdy.MyBottom&
public class MyBottom extends LinearLayout{
//定义自定义属性对应的控件
private TextView textview01,textview02,textview03;
private ImageView imageview01,imageview02,imageview03;
private String text01,text02,text03="";
private Drawable drawable01,drawable02,drawable03;
private int textColor01,textColor02,textColor03,backC
private float textS
private LinearLayout ll01,ll02,ll03;
public MyBottom(Context context, AttributeSet attrs) {
super(context, attrs);
//建立映射
TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attrs, R.styleable.Bottom);
text01=obtainStyledAttributes.getString(R.styleable.Bottom_oneText);
text02=obtainStyledAttributes.getString(R.styleable.Bottom_twoText);
**_System.out.println("-----------------------------"+text01+"-----------"+text02);_**
text03=obtainStyledAttributes.getString(R.styleable.Bottom_threeText);
drawable01=obtainStyledAttributes.getDrawable(R.styleable.Bottom_oneImageIcon);
drawable02=obtainStyledAttributes.getDrawable(R.styleable.Bottom_twoImageIcon);
drawable03=obtainStyledAttributes.getDrawable(R.styleable.Bottom_threeImageIcon);
textColor01=obtainStyledAttributes.getColor(R.styleable.Bottom_oneTextColor, 0);
textColor02=obtainStyledAttributes.getColor(R.styleable.Bottom_twoTextColor, 0);
textColor03=obtainStyledAttributes.getColor(R.styleable.Bottom_ThreeTextColor, 0);
backColor=obtainStyledAttributes.getColor(R.styleable.Bottom_backColor, 0);
textSize=obtainStyledAttributes.getDimension(R.styleable.Bottom_oneTextSize, 0);
obtainStyledAttributes.recycle();
//实例化控件
textview01=new TextView(context);
textview02=new TextView(context);
textview03=new TextView(context);
imageview01=new ImageView(context);
imageview02=new ImageView(context);
imageview03=new ImageView(context);
textview01.setText(text01);
textview02.setText(text02);
textview03.setText(text03);
textview01.setTextColor(textColor01);
textview02.setTextColor(textColor02);
textview03.setTextColor(textColor03);
textview01.setTextSize(textSize);
textview02.setTextSize(textSize);
textview03.setTextSize(textSize);
imageview01.setBackground(drawable01);
imageview02.setBackground(drawable02);
imageview03.setBackground(drawable03);
LayoutParams llParams=new LayoutParams(80, LayoutParams.FILL_PARENT );
LayoutParams llParams02=new LayoutParams(150, LayoutParams.FILL_PARENT);
ll01=new LinearLayout(context);
ll02=new LinearLayout(context);
ll03=new LinearLayout(context);
ll01.setBackgroundColor(textColor01);
ll02.setBackgroundColor(textColor02);
setOrientation(LinearLayout.HORIZONTAL);
LayoutParams textParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(textview01,llParams);
addView(textview02,llParams02);
自定义的属性
在自定义控件中使用 xml中赋值是也没报错
但是在自定义的view中使用
打印结果为null
是值引用不过来吗
按赞数排序
你打 syso的地方他还没初始化,能不是null么
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!
按理说应该是可以的,为什么呢?你调试出现什么问题么?查看下obtainStyledAttributes 的属性。
找到原因啦吗,能说一下嘛
其他相关推荐Android自定义控件属性详解_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Android自定义控件属性详解
来源:Linux社区&
作者:lincyang
1. reference:参考某一资源ID。&&& (1)属性定义:&&&&&&&&&&&& &declare-styleable name = "名称"&&&&&&&&&&&&&&&&&&& &attr name = "background" format = "reference" /&&&&&&&&&&&& &/declare-styleable&&&&& (2)属性使用:&&&&&&&&&&&&& &ImageView&&&&&&&&&&&&&&&&&&&& :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 = ""& &&&&&&&&&&&&&&&&&& 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 = ""   &&&&&&&&&&&& 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 = ""&&&&&&&&&&&&&&&&&&& 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相关信息见 专题页面
相关资讯 & & &
& (11/06/:26)
& (01/20/:15)
& (05/01/:29)
& (03/06/:38)
& (02/18/:27)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款}

我要回帖

更多关于 android 自定义控件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信