Android自定义view
来源:互联网 发布:64码高清网络电视官方 编辑:程序博客网 时间:2024/06/05 21:56
Android自定义view有人会分为自定义view和viewgroup两种,但目前我自己感觉可以分为继承view和继承Android自有的控件这两种,例如我们需要一个全新的控件会继承view或者我们只需要在某些现有的控件基础上修改即可。
这里先说一下自定义view的自定属性
例如我们现在自定义一个MyButton
现在res文件下的values新建一个attr文件,用来声明自己定义的属性
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyButton"> <attr name="reference" format="reference"/> <attr name="dimension" format="dimension"/> </declare-styleable></resources>
然后在MyButton代码中获取
package yasin.com.diyview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.util.AttributeSet;import android.widget.Button;/** * Created by Yasin on 2016/6/3. */public class MyButton extends Button { int reference,dp; public MyButton(Context context) { super(context); } public MyButton(Context context, AttributeSet attrs) { super(context, attrs); TypedArray attr = context.obtainStyledAttributes(R.styleable.MyButton); reference = attr.getResourceId(R.styleable.MyButton_reference,R.mipmap.ic_launcher); dp = attr.getDimensionPixelOffset(R.styleable.MyButton_dimension,10); attr.recycle();//释放资源 } public MyButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}
在布局文件中使用
这里重点提一下xml的第四行代码,我们需要自己声明一个资源路径,另外一定要自己命名(不能命名为android,这样和系统的冲突),在自定义的view中便可以声明自己的变量了
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:myattr="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="yasin.com.diyview.MainActivity"> <yasin.com.diyview.MyButton android:id="@+id/mybutton" myattr:dimension="10dp" myattr:reference="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
接下来的在java代码中的使用就很简单了
package yasin.com.diyview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;public class MainActivity extends AppCompatActivity { MyButton myButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myButton = (MyButton) findViewById(R.id.mybutton); Log.e("yasin",""+myButton.dp); }}
这里对自定义属性的类别进行介绍
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" />
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" />
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" />
dimension:尺寸值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /> </declare-styleable>
(2)属性使用:
<Button android:layout_width = "42dip" android:layout_height = "42dip" />
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" />
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" />
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" />
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" />
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>
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>
注意:
属性定义时可以指定多种类型值。
- Android View---自定义View
- Android View---自定义View
- Android 自定义View 之 自定义View属性
- 【自定义View系列】android自定义View概述
- Android自定义view自定义属性
- Android自定义控件 -- 自定义View
- android自定义view(自定义数字键盘)
- Android自定义View-自定义属性
- Android自定义View-自定义属性
- Android 自定义View
- Android 自定义 View
- android自定义View
- Android 中自定义 view
- android 自定义view组件
- Android 自定义 View
- android 自定义view
- Android:如何自定义View
- android 自定义View
- STL程序实例
- UESTC 490 Swap Game (特殊的求解逆序对数)
- 算法--动态规划
- 安卓实现随时获取Context(来自《第一行代码》)
- 二叉树--trie树
- Android自定义view
- LeetCode第23题之Merge k Sorted Lists
- Ajax解决IE浏览器兼容问题
- hdu5698瞬间移动+杨辉三角+LUCAS
- 解决tomcat启动Socket监听端口死循环被hold问题
- 2、.getcom.mchange.v2.c3p0.impl.NewProxyConnection cannot becast to com.mysql.jdbc.Connection
- A Two-Stage Ensemble of Diverse Models for Advertisement Ranking in KDD Cup 2012
- Java_【第一个小游戏-单词拼写字母排序】
- Java保留两位小数问题