自定义View添加自定义属性详细

简介:

自定义view经常需要设置一些属性, 很多人应该都知道,属性都是写在res/values文件夹下一个自定义的文件夹,但是通常很多人都起名attrs,

看下他的写法

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyView">
        <attr name="textColor" format="color"></attr>
        <attr name="textSize" format="dimension"></attr>
        <attr name="background" format="reference"></attr>
        <attr name="text" format="string"></attr>
    </declare-styleable>
    <declare-styleable name="rotateTextView">
        <attr name="rotate" format="integer"></attr>
    </declare-styleable>

</resources>

这是我写的一个自定义TextView 跟一个旋转了一定角度的TextView添加的属性;

很多人在使用这个的时候都是在自定义的view中通过

TypedArray  array  =context.obtainStyledAttributes(attrs, R.styleable.MyView);
        int color  =array.getColor(R.styleable.MyView_textColor, 0XFF00FF00);
        float textSize    =array.getDimension(R.styleable.MyView_textSize, 22);
        float degressSize =attrs.getAttributeFloatValue("http://www.ywlx.net/apk/res/easymobi", "rotate", 0);

这样的方式获取到这个属性来进行设置,然后绘制,在layout中也可以,但是我在第一次弄的时 候就是不行,然后就查了点东西: 

如果要在layout中使用的话,那么你必须要给相应的自定义view添加相应的命名空间,如果不添加时不会认识你的熟悉的,也就不能再布局中设置:

<LinearLayout 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"
    android:orientation="vertical" >
    <com.example.myview.RotateTextView
            xmlns:myview="http://schemas.android.com/apk/res/com.example.myview"
        android:id="@+id/rotateTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="fgfghghghg"
        myview:rotate="50"
        android:background="@drawable/ic_launcher"
        ></com.example.myview.RotateTextView>
<!-- <com.example.myview.MTextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="qweqweqweq"
        android:textColor="#000000"
        ></com.example.myview.MTextView> -->

<!-- <com.example.myview.MyListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    ></com.example.myview.MyListView> -->
</LinearLayout>

命名空间的规则就是使用你当前包的包名xmlns:myview="http://schemas.android.com/apk/res/com.example.myview"  
这个是用来定义命名空间用的,myview代表的命名空间
com.example.myview表示该命名空间用于哪个包 

然后你就可以使用你定义的属性;

自定义view学习中。

 一个自定义的倾斜textView:http://download.csdn.net/detail/u012808234/8524853



相关文章
|
4月前
自定义View实现点击事件
自定义View实现点击事件
36 2
|
容器
PopupWindow(悬浮框)的基本使用
后一个用于显示信息的UI控件——PopupWindow(悬浮框),如果你想知道他长什么样子,你可以打开你手机的QQ,长按列表中的某项,这个时候后弹出一个黑色的小对话框,这种就是PopupWindow了,和AlertDialog对话框不同的是,他的位置可以是随意的; 另外AlertDialog是非堵塞线程的,而PopupWindow则是堵塞线程的!而官方有这样一句话来介绍PopupWindow: A popup window that can be used to display an arbitrary view. The popup window is a floating conta
152 0
ScrollView和HorizontalScrollView无法设置点击事件的源码解析
最近的开发过程中,发现存在ScrollView和HorizontalScrollView无法设置点击事件的现象。 我们知道,通常在设置点击事件时,位于View树上方的子View的OnClickListener,会优先于父View的OnClickListener执行。 开发过程中我们会经常使用类似的方式来给布局设置点击事件,比如给ListView的Item背景设置OnClickListener,用于点击item空白区域的跳转操作;然后再给item内部的子元素分别设置OnClickListener用于各自不同的点击操作。
|
XML Android开发 数据格式
Android自定义控件(十二)——自定义属性及应用
Android自定义控件(十二)——自定义属性及应用
166 0
Android自定义控件(十二)——自定义属性及应用
|
XML Android开发 数据格式
Android动画一:Activity过渡动画详细实现原理
虽然 Android 5.0 之后推出了新的过渡动画方式,但通常只是用于特定的场合使用,activity.overridePendingTransition() 通用方式的过渡动画还是很常用。 原理分析 startActivity(Intent(this,SecondActivity::class.
7541 0
|
前端开发
自定义View的各个重要的类和属性
        1 、Path类详解 2、Paint详解 3、Canvas详解 4 、绘制雷达图
609 0
|
Android开发
Android布局属性详解
RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInpare...
1304 0
|
XML 前端开发 Android开发