自定义View(二)增加View的属性
增加View的属性有两种方法 1、在View类中添加 2、在xml资源文件中添加
一、在View类中添加 例:实现一个带文字的图片
- public class MyView extends View {
- private String mtext;
- private int msrc;
- public MyView(Context context) {
- super(context);
- }
- public MyView(Context context, AttributeSet attrs) {
- super(context, attrs);
- int resourceId = 0;
- int textId = attrs.getAttributeResourceValue(null, "Text",0);
- int srcId = attrs.getAttributeResourceValue(null, "Src", 0);
- mtext = context.getResources().getText(textId).toString();
- msrc = srcId;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- Paint paint = new Paint();
- paint.setColor(Color.RED);
- InputStream is = getResources().openRawResource(msrc);
- Bitmap mBitmap = BitmapFactory.decodeStream(is);
- int bh = mBitmap.getHeight();
- int bw = mBitmap.getWidth();
- canvas.drawBitmap(mBitmap, 0,0, paint);
- //canvas.drawCircle(40, 90, 15, paint);
- canvas.drawText(mtext, bw/2, 30, paint);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <com.example.myimageview2.MyView
- android:id="@+id/myView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- Text="@string/hello_world"
- Src="@drawable/xh"/>
- </LinearLayout>
二、 在xml资源文件中添加
- public class MyImageView extends LinearLayout {
- public MyImageView(Context context) {
- super(context);
- }
- public MyImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- int resourceId = -1;
- TypedArray typedArray = context.obtainStyledAttributes(attrs,
- R.styleable.MyImageView);
- ImageView iv = new ImageView(context);
- TextView tv = new TextView(context);
- int N = typedArray.getIndexCount();
- for (int i = 0; i < N; i++) {
- int attr = typedArray.getIndex(i);
- switch (attr) {
- case R.styleable.MyImageView_Oriental:
- resourceId = typedArray.getInt(
- R.styleable.MyImageView_Oriental, 0);
- this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL
- : LinearLayout.VERTICAL);
- break;
- case R.styleable.MyImageView_Text:
- resourceId = typedArray.getResourceId(
- R.styleable.MyImageView_Text, 0);
- tv.setText(resourceId > 0 ? typedArray.getResources().getText(
- resourceId) : typedArray
- .getString(R.styleable.MyImageView_Text));
- break;
- case R.styleable.MyImageView_Src:
- resourceId = typedArray.getResourceId(
- R.styleable.MyImageView_Src, 0);
- iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);
- break;
- }
- }
- addView(iv);
- addView(tv);
- typedArray.recycle();
- }
- }
在values文件夹下的 attrs.xml中声明属性
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="MyImageView">
- <attr name="Text" format="reference|string"></attr>
- <attr name="Oriental" >
- <enum name="Horizontal" value="1"></enum>
- <enum name="Vertical" value="0"></enum>
- </attr>
- <attr name="Src" format="reference|integer"></attr>
- </declare-styleable>
- </resources>
在MainActivity中布局文件的使用 必须声明红色部分 才能在自定义的View中使用绿色部分的属性
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <com.example.myimageview2.MyImageView
- android:id="@+id/myImageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- uview:Text="这是一个图片说明"
- uview:Src="@drawable/tw"
- uview:Oriental="Vertical">
- </com.example.myimageview2.MyImageView>
- </LinearLayout>
自定义View(二)增加View的属性的更多相关文章
- 自定义View(二)--继承自ViewGroup
自定义View包括很多种,上一次随笔中的那一种是完全继承自View,这次写的这个小Demo是继承自ViewGroup的,主要是将自定义View继承自ViewGroup的这个流程来梳理一下,这次的Dem ...
- Android View框架总结(二)View焦点
请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52263256 前言:View框架写到第六篇,发现前面第二篇竟然没有, ...
- Android开发艺术探索笔记——View(二)
Android开发艺术探索笔记--View(二) View的事件分发机制 学习资料: 1.Understanding Android Input Touch Events System Framewo ...
- Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件
一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上 咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: ...
- Android自定义View前传-View的三大流程-Measure
Android自定义View前传-View的三大流程-Measure 参考 <Android开发艺术探索> https://developer.android.google.cn/refe ...
- React Native组件(二)View组件解析
相关文章 React Native探索系列 React Native组件系列 前言 了解了RN的组件的生命周期后,我们接着来学习RN的具体的组件.View组件是最基本的组件,也是首先要掌握的组件,这一 ...
- React Native学习(二)之View
React Native组件解析(二)之View 0.JSX React的核心机制之一就是虚拟DOM:可以在内存中创建的虚拟DOM元素.React利用虚拟DOM来减少对实际DOM的操作从而提升性能. ...
- 【Android - 自定义View】之View的位置参数
View是Android中所有控件的基类,不管是简单的Button和TextView,还是复杂的RelativeLayout和ListView,其基类都是View类:ViewGroup也继承了View ...
- 自定义加载loading view动画组件的使用。
在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress 我写写使用步骤 自定义view(CircleProgress ...
随机推荐
- c#全局鼠标事件以及鼠标事件模拟
最近在编写Max插件时,其主容器FlowLayoutPanel由于隐藏了滚动条,要实现按住鼠标中键上下拖动的功能,因此尝试了全局鼠标事件.以及鼠标勾子,可惜由于Max不争气?都未能实现,于是代码报废, ...
- PHP中获取当前页面的完整URL
//获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br>"; #localhost//获取网页地址 echo $_SERVER['PHP ...
- cs107
基本类型:bool,char,short,int,long,float,double 对于char,short,int,long: 多字节类型赋值给少字节类型,对低字节的细节感兴趣,位模式拷贝. 少字 ...
- 用jquery解析JSON数据的方法以及字符串转换成json的3种方法
用jquery解析JSON数据的方法,作为jquery异步请求的传输对象,jquery请求后返回的结果是 json对象,这里考虑的都是服务器返回JSON形式的字符串的形式,对于利用JSONObject ...
- Jquery.cookie.js 源码和使用方法
jquery.cookie.js源码和使用方法 jQuery操作cookie的插件,大概的使用方法如下 $.cookie(‘the_cookie’); //读取Cookie值$.cookie(’the ...
- [Python] MySQLdb(即 MySQL-python 包)在 OS X 中安装指南
本文参考:http://www.cnblogs.com/ifantastic/archive/2013/04/13/3017677.html 安装环境:OS X 操作系统,Python 2.7.10 ...
- LCC
LCC: super vector:
- 封装、调用ajax
1.JavaScript代码 //封装ajaxfunction ajax(obj) { var xhr = new createXHR(); obj.url = obj.url + '?rand=' ...
- chroot directory
给 /A/B/C的C目录做chroot,要对C能读写,所以C目录不能做ROOT目录,对B做chroot. 设置C目录所有者为sftp 账户a,组也改为sftp组(这里a和sftp组都是之前建立好的sf ...
- splinter(python操作浏览器魔魁啊)
from splinter import Browser def main(): browser = Browser() browser.visit('http://google.com') brow ...