增加View的属性有两种方法    1、在View类中添加    2、在xml资源文件中添加

一、在View类中添加    例:实现一个带文字的图片

  1. public class MyView extends View {
  2.  
  3. private String mtext;
  4. private int msrc;
  5.  
  6. public MyView(Context context) {
  7. super(context);
  8. }
  9.  
  10. public MyView(Context context, AttributeSet attrs) {
  11. super(context, attrs);
  12. int resourceId = 0;
  13. int textId = attrs.getAttributeResourceValue(null, "Text",0);
  14. int srcId = attrs.getAttributeResourceValue(null, "Src", 0);
  15. mtext = context.getResources().getText(textId).toString();
  16. msrc = srcId;
  17. }
  18.  
  19. @Override
  20. protected void onDraw(Canvas canvas) {
  21. Paint paint = new Paint();
  22. paint.setColor(Color.RED);
  23. InputStream is = getResources().openRawResource(msrc);
  24. Bitmap mBitmap = BitmapFactory.decodeStream(is);
  25. int bh = mBitmap.getHeight();
  26. int bw = mBitmap.getWidth();
  27. canvas.drawBitmap(mBitmap, 0,0, paint);
  28. //canvas.drawCircle(40, 90, 15, paint);
  29. canvas.drawText(mtext, bw/2, 30, paint);
  30. }
  31. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <com.example.myimageview2.MyView
  8. android:id="@+id/myView1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. Text="@string/hello_world"
  12. Src="@drawable/xh"/>
  13.  
  14. </LinearLayout>

二、 在xml资源文件中添加

  1. public class MyImageView extends LinearLayout {
  2.  
  3. public MyImageView(Context context) {
  4. super(context);
  5. }
  6.  
  7. public MyImageView(Context context, AttributeSet attrs) {
  8. super(context, attrs);
  9. int resourceId = -1;
  10. TypedArray typedArray = context.obtainStyledAttributes(attrs,
  11. R.styleable.MyImageView);
  12. ImageView iv = new ImageView(context);
  13. TextView tv = new TextView(context);
  14. int N = typedArray.getIndexCount();
  15. for (int i = 0; i < N; i++) {
  16. int attr = typedArray.getIndex(i);
  17. switch (attr) {
  18. case R.styleable.MyImageView_Oriental:
  19. resourceId = typedArray.getInt(
  20. R.styleable.MyImageView_Oriental, 0);
  21. this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL
  22. : LinearLayout.VERTICAL);
  23. break;
  24. case R.styleable.MyImageView_Text:
  25. resourceId = typedArray.getResourceId(
  26. R.styleable.MyImageView_Text, 0);
  27. tv.setText(resourceId > 0 ? typedArray.getResources().getText(
  28. resourceId) : typedArray
  29. .getString(R.styleable.MyImageView_Text));
  30. break;
  31. case R.styleable.MyImageView_Src:
  32. resourceId = typedArray.getResourceId(
  33. R.styleable.MyImageView_Src, 0);
  34. iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);
  35. break;
  36. }
  37. }
  38. addView(iv);
  39. addView(tv);
  40. typedArray.recycle();
  41. }
  42. }

在values文件夹下的  attrs.xml中声明属性

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4. <declare-styleable name="MyImageView">
  5. <attr name="Text" format="reference|string"></attr>
  6. <attr name="Oriental" >
  7. <enum name="Horizontal" value="1"></enum>
  8. <enum name="Vertical" value="0"></enum>
  9. </attr>
  10. <attr name="Src" format="reference|integer"></attr>
  11. </declare-styleable>
  12.  
  13. </resources>

在MainActivity中布局文件的使用   必须声明红色部分 才能在自定义的View中使用绿色部分的属性

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity" >
  8.  
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="@string/hello_world" />
  13.  
  14. <com.example.myimageview2.MyImageView
  15. android:id="@+id/myImageView1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. uview:Text="这是一个图片说明"
  19. uview:Src="@drawable/tw"
  20. uview:Oriental="Vertical">
  21. </com.example.myimageview2.MyImageView>
  22.  
  23. </LinearLayout>

自定义View(二)增加View的属性的更多相关文章

  1. 自定义View(二)--继承自ViewGroup

    自定义View包括很多种,上一次随笔中的那一种是完全继承自View,这次写的这个小Demo是继承自ViewGroup的,主要是将自定义View继承自ViewGroup的这个流程来梳理一下,这次的Dem ...

  2. Android View框架总结(二)View焦点

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52263256 前言:View框架写到第六篇,发现前面第二篇竟然没有, ...

  3. Android开发艺术探索笔记——View(二)

    Android开发艺术探索笔记--View(二) View的事件分发机制 学习资料: 1.Understanding Android Input Touch Events System Framewo ...

  4. Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件

    一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上 咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: ...

  5. Android自定义View前传-View的三大流程-Measure

    Android自定义View前传-View的三大流程-Measure 参考 <Android开发艺术探索> https://developer.android.google.cn/refe ...

  6. React Native组件(二)View组件解析

    相关文章 React Native探索系列 React Native组件系列 前言 了解了RN的组件的生命周期后,我们接着来学习RN的具体的组件.View组件是最基本的组件,也是首先要掌握的组件,这一 ...

  7. React Native学习(二)之View

    React Native组件解析(二)之View 0.JSX React的核心机制之一就是虚拟DOM:可以在内存中创建的虚拟DOM元素.React利用虚拟DOM来减少对实际DOM的操作从而提升性能. ...

  8. 【Android - 自定义View】之View的位置参数

    View是Android中所有控件的基类,不管是简单的Button和TextView,还是复杂的RelativeLayout和ListView,其基类都是View类:ViewGroup也继承了View ...

  9. 自定义加载loading view动画组件的使用。

    在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress 我写写使用步骤 自定义view(CircleProgress  ...

随机推荐

  1. c#全局鼠标事件以及鼠标事件模拟

    最近在编写Max插件时,其主容器FlowLayoutPanel由于隐藏了滚动条,要实现按住鼠标中键上下拖动的功能,因此尝试了全局鼠标事件.以及鼠标勾子,可惜由于Max不争气?都未能实现,于是代码报废, ...

  2. PHP中获取当前页面的完整URL

    //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br>"; #localhost//获取网页地址 echo $_SERVER['PHP ...

  3. cs107

    基本类型:bool,char,short,int,long,float,double 对于char,short,int,long: 多字节类型赋值给少字节类型,对低字节的细节感兴趣,位模式拷贝. 少字 ...

  4. 用jquery解析JSON数据的方法以及字符串转换成json的3种方法

    用jquery解析JSON数据的方法,作为jquery异步请求的传输对象,jquery请求后返回的结果是 json对象,这里考虑的都是服务器返回JSON形式的字符串的形式,对于利用JSONObject ...

  5. Jquery.cookie.js 源码和使用方法

    jquery.cookie.js源码和使用方法 jQuery操作cookie的插件,大概的使用方法如下 $.cookie(‘the_cookie’); //读取Cookie值$.cookie(’the ...

  6. [Python] MySQLdb(即 MySQL-python 包)在 OS X 中安装指南

    本文参考:http://www.cnblogs.com/ifantastic/archive/2013/04/13/3017677.html 安装环境:OS X 操作系统,Python 2.7.10 ...

  7. LCC

    LCC: super vector:

  8. 封装、调用ajax

    1.JavaScript代码 //封装ajaxfunction ajax(obj) { var xhr = new createXHR(); obj.url = obj.url + '?rand=' ...

  9. chroot directory

    给 /A/B/C的C目录做chroot,要对C能读写,所以C目录不能做ROOT目录,对B做chroot. 设置C目录所有者为sftp 账户a,组也改为sftp组(这里a和sftp组都是之前建立好的sf ...

  10. splinter(python操作浏览器魔魁啊)

    from splinter import Browser def main(): browser = Browser() browser.visit('http://google.com') brow ...