Android 之 View(一)简单详述
来源:互联网 发布:淘宝订单清洗会降权吗 编辑:程序博客网 时间:2024/06/04 18:25
好久不曾写东西了.. 最近心情很舒畅,加上工作稍微轻松了些..整理下最近手里的东西.
Android 之 ViewView 是 Android 中最基本的显示类,是所有屏幕显示控件的超类. 包含了所有屏幕类型.
基于view的控件都有一个画布canvas。最终最终的展示都会通过onDraw(Canvas c)方法把画布画出来。
我们可以自定义自己的View 继承 android.view.View,重写 onDraw 方法,将自己想要的效果展示出来。
自定义 view 的使用方式
1. 直接在 activity 的布局文件中使用
<com.example.viewdemo.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
2. 在 activity 中去 new
View v = new MyView(this);
this.setContentView(v);
自定义view的构造实现和构造参数
构造方法 MyView(Context contextview) context 代表 调用的Activity 对象。在Activity中调用是传参this---->> new MyView(this);
构造方法 MyView(Context context, AttributeSet attrs)
AttributeSet 的使用
1. 在res/values 文件下定义一个attrs.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="View1">
<attr name="MyColor" format="color" />
<attr name="MySize" format="dimension" />
</declare-styleable>
</resources>
2. 在构造方法中调用自定义的文件attrs.xml 中的 View1
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.View1);
int mycolor = ta.getColor(R.styleable.View1_MyColor, 0XFFFFFFFF);
3. 直接在layout 文件中加入引用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
xmlns:app="http://schemas.android.com/apk/res/com.example.viewdemo">
首先要引入 xmlns:app="http://schemas.android.com/apk/res/com.example.viewdemo">
app 是调用自定义属性的前缀,com.example.viewdemo 项目的报名
<com.example.viewdemo.View1
android:layout_width="match_parent"
android:layout_height="match_parent"
app:MyColor="#FFFFFF"
app:MySize="989"
/>
未完.... 待续。
- Android 之 View(一)简单详述
- android 之View 一
- Android之activity详述
- android自定义view之地图(一)
- Android进阶之自定义view(一)
- 【Android进阶之自定义View(一)】
- Android事件分发之View(一)
- android 自定义view之概述(一)
- android 自定义view之(一) Creating a View Class
- android 之 custom view(一)
- Android自定义View 一<最简单的自定义View>
- Android开发之自定义View专题(一):自定义柱形图
- Android自定义View之吃豆人动画(一)
- Android:自定义View 初步之旅随记(一)
- Android进阶之——自定义view(一)
- Android自定义View之基本API(一)
- Android自定义View之刻度尺滑动功能(一)
- Android 深入(一)- 自定义View之BottomTitleImageView
- [C#] 如何将String类型转换成任意基本类型
- 思维逻辑的清晰
- VBS U盘插入提示
- vi/vim常用命令
- Kobject模型之linux内核源码
- Android 之 View(一)简单详述
- Query的list()和iterate()方法的区别
- 我对插入排序算法的理解。
- android22_Android的布局和Intent笔记和常用实例
- C语言第三次课堂作业
- RMQ poj3264
- vsftpd服务
- 自动循环播放图片
- 用URLLoader读取本地txt,xml等格式文件