Android 深入理解Android中的自定义属性

Android 深入理解Android中的自定义属性

标签: 自定义ViewTypedArray
31740人阅读 评论(60) 收藏 举报
分类:

目录(?)[+]

转载请标明出处:
http://blog.csdn.net/lmj623565791/article/details/45022631
本文出自:【张鸿洋的博客】

1、引言

对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现:

  1. 自定义一个CustomView(extends View )类
  2. 编写values/attrs.xml,在其中编写styleable和item等标签元素
  3. 在布局文件中CustomView使用自定义的属性(注意namespace)
  4. 在CustomView的构造方法中通过TypedArray获取

ps:如果你对上述几个步骤不熟悉,建议先熟悉下,再继续~

那么,我有几个问题:

  • 以上步骤是如何奏效的?
  • styleable 的含义是什么?可以不写嘛?我自定义属性,我声明属性就好了,为什么一定要写个styleable呢?
  • 如果系统中已经有了语义比较明确的属性,我可以直接使用嘛?
  • 构造方法中的有个参数叫做AttributeSet
    (eg: MyTextView(Context context, AttributeSet attrs) )这个参数看名字就知道包含的是参数的数组,那么我能不能通过它去获取我的自定义属性呢?
  • TypedArray是什么鬼?从哪冒出来的,就要我去使用?

恩,针对这几个问题,大家可以考虑下,如何回答呢?还是说:老子会背上述4个步骤就够了~~

2、常见的例子

接下来通过例子来回答上述问题,问题的回答顺序不定~~大家先看一个常见的例子,即上述几个步骤的代码化。

  • 自定义属性的声明文件
<code class="language-xml hljs  has-numbering">    <span class="hljs-pi"><?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag"><<span class="hljs-title">resources</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">declare-styleable</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"test"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">attr</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"text"</span> <span class="hljs-attribute">format</span>=<span class="hljs-value">"string"</span> /></span>
        <span class="hljs-tag"><<span class="hljs-title">attr</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"testAttr"</span> <span class="hljs-attribute">format</span>=<span class="hljs-value">"integer"</span> /></span>
    <span class="hljs-tag"></<span class="hljs-title">declare-styleable</span>></span>

<span class="hljs-tag"></<span class="hljs-title">resources</span>></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
  • 自定义View类
<code class="language-java hljs  has-numbering"><span class="hljs-keyword">package</span> com.example.test;

<span class="hljs-keyword">import</span> android.content.Context;
<span class="hljs-keyword">import</span> android.content.res.TypedArray;
<span class="hljs-keyword">import</span> android.util.AttributeSet;
<span class="hljs-keyword">import</span> android.util.Log;
<span class="hljs-keyword">import</span> android.view.View;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyTextView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">View</span> {</span>

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String TAG = MyTextView.class.getSimpleName();

    <span class="hljs-keyword">public</span> <span class="hljs-title">MyTextView</span>(Context context, AttributeSet attrs) {
        <span class="hljs-keyword">super</span>(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);

        String text = ta.getString(R.styleable.test_testAttr);
        <span class="hljs-keyword">int</span> textAttr = ta.getInteger(R.styleable.test_text, -<span class="hljs-number">1</span>);

        Log.e(TAG, <span class="hljs-string">"text = "</span> + text + <span class="hljs-string">" , textAttr = "</span> + textAttr);

        ta.recycle();
    }

}</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li></ul>
  • 布局文件中使用
<code class="language-xml hljs  has-numbering"><span class="hljs-tag"><<span class="hljs-title">RelativeLayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>
    <span class="hljs-attribute">xmlns:tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span>
    <span class="hljs-attribute">xmlns:zhy</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/com.example.test"</span>
    <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span>
    <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span> ></span>

    <span class="hljs-tag"><<span class="hljs-title">com.example.test.MyTextView
</span>        <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"100dp"</span>
        <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"200dp"</span>
        <span class="hljs-attribute">zhy:testAttr</span>=<span class="hljs-value">"520"</span>
        <span class="hljs-attribute">zhy:text</span>=<span class="hljs-value">"helloworld"</span> /></span>

<span class="hljs-tag"></<span class="hljs-title">RelativeLayout</span>></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li></ul>

ok,大家花3s扫一下,运行结果为:

<code class="language-java hljs  has-numbering"> MyTextView: text = helloworld , textAttr = <span class="hljs-number">520</span></code><ul style="display: block;" class="pre-numbering"><li>1</li></ul>

应该都不意外吧,注意下,我的styleable的name写的是test,所以说这里并不要求一定是自定义View的名字。

3、AttributeSet与TypedArray

下面考虑:

构造方法中的有个参数叫做AttributeSet(eg: MyTextView(Context context, AttributeSet attrs) )这个参数看名字就知道包含的是参数的集合,那么我能不能通过它去获取我的自定义属性呢?

首先AttributeSet中的确保存的是该View声明的所有的属性,并且外面的确可以通过它去获取(自定义的)属性,怎么做呢?
其实看下AttributeSet的方法就明白了,下面看代码。

<code class="language-java hljs  has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-title">MyTextView</span>(Context context, AttributeSet attrs) {
        <span class="hljs-keyword">super</span>(context, attrs);

        <span class="hljs-keyword">int</span> count = attrs.getAttributeCount();
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < count; i++) {
            String attrName = attrs.getAttributeName(i);
            String attrVal = attrs.getAttributeValue(i);
            Log.e(TAG, <span class="hljs-string">"attrName = "</span> + attrName + <span class="hljs-string">" , attrVal = "</span> + attrVal);
        }

        <span class="hljs-comment">// ==>use typedarray ...</span>

    }
</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li></ul>

输出:

<code class="language-java hljs  has-numbering">MyTextView(<span class="hljs-number">4136</span>): attrName = layout_width , attrVal = <span class="hljs-number">100.0</span>dip
MyTextView(<span class="hljs-number">4136</span>): attrName = layout_height , attrVal = <span class="hljs-number">200.0</span>dip
MyTextView(<span class="hljs-number">4136</span>): attrName = text , attrVal = helloworld
MyTextView(<span class="hljs-number">4136</span>): attrName = testAttr , attrVal = <span class="hljs-number">520</span>
</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

结合上面的布局文件,你发现了什么?
我擦,果然很神奇,真的获得所有的属性,恩,没错,通过AttributeSet可以获得布局文件中定义的所有属性的key和value(还有一些方法,自己去尝试),那么是不是说TypedArray这个鬼可以抛弃了呢?答案是:NO!

现在关注下一个问题:

TypedArray是什么鬼?从哪冒出来的,就要我去使用?

我们简单修改下,布局文件中的MyTextView的属性。

<code class="language-xml hljs  has-numbering"><span class="hljs-tag"><<span class="hljs-title">com.example.test.MyTextView
</span>        <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"@dimen/dp100"</span>
        <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"@dimen/dp200"</span>
        <span class="hljs-attribute">zhy:testAttr</span>=<span class="hljs-value">"520"</span>
        <span class="hljs-attribute">zhy:text</span>=<span class="hljs-value">"@string/hello_world"</span> /></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

现在再次运行的结果是:

<code class="language-java hljs  has-numbering">MyTextView(<span class="hljs-number">4692</span>): attrName = layout_width , attrVal = @<span class="hljs-number">2131165234</span>
MyTextView(<span class="hljs-number">4692</span>): attrName = layout_height , attrVal = @<span class="hljs-number">2131165235</span>
MyTextView(<span class="hljs-number">4692</span>): attrName = text , attrVal = @<span class="hljs-number">2131361809</span>
MyTextView(<span class="hljs-number">4692</span>): attrName = testAttr , attrVal = <span class="hljs-number">520</span>
>>use typedarray
MyTextView(<span class="hljs-number">4692</span>): text = Hello world! , textAttr = <span class="hljs-number">520</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>

发现了什么?通过AttributeSet获取的值,如果是引用都变成了@+数字的字符串。你说,这玩意你能看懂么?那么你看看最后一行使用TypedArray获取的值,是不是瞬间明白了什么。

TypedArray其实是用来简化我们的工作的,比如上例,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程。

贴一下:如果通过AttributeSet获取最终的像素值的过程:

<code class="language-java hljs  has-numbering"><span class="hljs-keyword">int</span> widthDimensionId =  attrs.getAttributeResourceValue(<span class="hljs-number">0</span>, -<span class="hljs-number">1</span>);
        Log.e(TAG, <span class="hljs-string">"layout_width= "</span>+getResources().getDimension(widthDimensionId));
</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>

ok,现在别人问你TypedArray存在的意义,你就可以告诉他了。

4、declare-styleable

我们已经解决了两个问题,接下来,我们看看布局文件,我们有一个属性叫做:zhy:text
总所周知,系统提供了一个属性叫做:android:text,那么我觉得直接使用android:text更nice,这样的话,考虑问题:

如果系统中已经有了语义比较明确的属性,我可以直接使用嘛?

答案是可以的,怎么做呢?
直接在attrs.xml中使用android:text属性。

<code class="language-xml hljs  has-numbering">    <span class="hljs-tag"><<span class="hljs-title">declare-styleable</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"test"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">attr</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"android:text"</span> /></span>
        <span class="hljs-tag"><<span class="hljs-title">attr</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"testAttr"</span> <span class="hljs-attribute">format</span>=<span class="hljs-value">"integer"</span> /></span>
    <span class="hljs-tag"></<span class="hljs-title">declare-styleable</span>></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>

注意,这里我们是使用已经定义好的属性,不需要去添加format属性(注意声明和使用的区别,差别就是有没有format)。
然后在类中这么获取:ta.getString(R.styleable.test_android_text);布局文件中直接android:text="@string/hello_world"即可。

这里提一下,系统中定义的属性,其实和我们自定义属性的方式类似,你可以在sdk/platforms/android-xx/data/res/values该目录下看到系统中定义的属性。然后你可以在系统提供的View(eg:TextView)的构造方法中发现TypedArray获取属性的代码(自己去看一下)。

ok,接下来,我在想,既然declare-styleable这个标签的name都能随便写,这么随意的话,那么考虑问题:

styleable 的含义是什么?可以不写嘛?我自定义属性,我声明属性就好了,为什么一定要写个styleable呢?

其实的确是可以不写的,怎么做呢?

  • 首先删除declare-styleable的标签

那么现在的attrs.xml为:

<code class="language-xml hljs  has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag"><<span class="hljs-title">resources</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">attr</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"testAttr"</span> <span class="hljs-attribute">format</span>=<span class="hljs-value">"integer"</span> /></span>
<span class="hljs-tag"></<span class="hljs-title">resources</span>></span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>

哟西,so清爽~
* MyTextView实现

<code class="language-java hljs  has-numbering"><span class="hljs-keyword">package</span> com.example.test;

<span class="hljs-keyword">import</span> android.content.Context;
<span class="hljs-keyword">import</span> android.content.res.TypedArray;
<span class="hljs-keyword">import</span> android.util.AttributeSet;
<span class="hljs-keyword">import</span> android.util.Log;
<span class="hljs-keyword">import</span> android.view.View;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyTextView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">View</span> {</span>

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String TAG = MyTextView.class.getSimpleName();

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span>[] mAttr = { android.R.attr.text, R.attr.testAttr };
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> ATTR_ANDROID_TEXT = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> ATTR_TESTATTR = <span class="hljs-number">1</span>;

    <span class="hljs-keyword">public</span> <span class="hljs-title">MyTextView</span>(Context context, AttributeSet attrs) {
        <span class="hljs-keyword">super</span>(context, attrs);

        <span class="hljs-comment">// ==>use typedarray</span>
        TypedArray ta = context.obtainStyledAttributes(attrs, mAttr);

        String text = ta.getString(ATTR_ANDROID_TEXT);
        <span class="hljs-keyword">int</span> textAttr = ta.getInteger(ATTR_TESTATTR, -<span class="hljs-number">1</span>);
        <span class="hljs-comment">//输出 text = Hello world! , textAttr = 520</span>
        Log.e(TAG, <span class="hljs-string">"text = "</span> + text + <span class="hljs-string">" , textAttr = "</span> + textAttr);

        ta.recycle();
    }

}
</code>

貌似多了些代码,可以看到我们声明了一个int数组,数组中的元素就是我们想要获取的attr的id。并且我们根据元素的在数组中的位置,定义了一些整形的常量代表其下标,然后通过TypedArray进行获取。
可以看到,我们原本的:

<code class="language-xml hljs  has-numbering">R.styleable.test => mAttr
R.styleable.test_text => ATTR_ANDROID_TEXT(0)
R.styleable.test_testAttr => ATTR_TESTATTR(1)</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li></ul>

那么其实呢?android在其内部也会这么做,按照传统的写法,它会在R.java生成如下代码:

<code class="language-java hljs  has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">attr</span> {</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> testAttr=<span class="hljs-number">0x7f0100a9</span>;
    }
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">styleable</span> {</span>
     <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> test_android_text = <span class="hljs-number">0</span>;
     <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> test_testAttr = <span class="hljs-number">1</span>;
      <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span>[] test = {
            <span class="hljs-number">0x0101014f</span>, <span class="hljs-number">0x7f0100a9</span>
        };
    }</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul>

ok,根据上述你应该发现了什么。styleale的出现系统可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作(想想如果一堆属性,自己编写常量,你得写成什么样的代码)。那么大家肯定还知道declare-styleable的name属性,一般情况下写的都是我们自定义View的类名。主要为了直观的表达,该declare-styleable的属性,都是改View所用的。

其实了解该原理是有用的,详见:Android 自定义控件 优雅实现元素间的分割线

ok,现在5个问题,回答了4个,第一个问题:

自定义属性的几个步骤是如何奏效的?

恩,上述以及基本涵盖了这个问题的答案,大家自己总结,所以:略。

总结下今天的博客。

  • attrs.xml里面的declare-styleable以及item,android会根据其在R.java中生成一些常量方便我们使用(aapt干的),本质上,我们可以不声明declare-styleable仅仅声明所需的属性即可。
  • 我们在View的构造方法中,可以通过AttributeSet去获得自定义属性的值,但是比较麻烦,而TypedArray可以很方便的便于我们去获取。
  • 我们在自定义View的时候,可以使用系统已经定义的属性。

近期的更新计划:自定义View的一些细节相关的Blog(重点会在交互上),Android最佳实践相关的文章,framework相关的一些文章,敬请期待



自定义控件在android中无处不见,自定义控件给了我们很大的方便。比如说,一个视图为imageview  ,imagebutton ,textview 等诸多控件的组合,用的地方有很多,我们不可能每次都来写3个的组合,既浪费时间,效率又低。在这种情况下,我们就可以自定义一个view来替换他们,不仅提升了效率并且在xml中运用也是相当的美观。羡慕

一、控件自定义属性介绍

以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。

1. reference:参考某一资源ID。

示例:
  1. <declare-styleable name = "名称">  
  2.        <attr name = "background" format = "reference" />  
  3.        <attr name = "src" format = "reference" />  
  4. </declare-styleable>  
2. color:颜色值。
示例:
  1. <declare-styleable name = "名称">  
  2.        <attr name = "textColor" format = "color" />  
  3. </declare-styleable>  
3. boolean:布尔值。
示例:
  1. <declare-styleable name = "名称">  
  2.        <attr name = "focusable" format = "boolean" />  
  3. </declare-styleable>  
4. dimension:尺寸值。
示例:
  1. <declare-styleable name = "名称">  
  2.        <attr name = "layout_width" format = "dimension" />  
  3. </declare-styleable>  
5. float:浮点值。
示例:
  1. <declare-styleable name = "名称">  
  2.        <attr name = "fromAlpha" format = "float" />  
  3.        <attr name = "toAlpha" format = "float" />  
  4. </declare-styleable>  
6. integer:整型值。
示例:
  1. <declare-styleable name = "名称">  
  2.        <attr name = "frameDuration" format="integer" />  
  3.        <attr name = "framesCount" format="integer" />  
  4. </declare-styleable>  
7. string:字符串。
示例:
  1. <declare-styleable name = "名称">  
  2.        <attr name = "text" format = "string" />  
  3. </declare-styleable>  
8. fraction:百分数。
示例:
  1. <declare-styleable name="名称">  
  2.        <attr name = "pivotX" format = "fraction" />  
  3.        <attr name = "pivotY" format = "fraction" />  
  4. </declare-styleable>  
9. enum:枚举值。
示例:
  1. <declare-styleable name="名称">  
  2.        <attr name="orientation">  
  3.               <enum name="horizontal" value="0" />  
  4.               <enum name="vertical" value="1" />  
  5.        </attr>             
  6. </declare-styleable>  

10. flag:位或运算。

示例:
  1. <declare-styleable name="名称">  
  2.        <attr name="windowSoftInputMode">  
  3.                <flag name = "stateUnspecified" value = "0" />  
  4.                <flag name = "stateUnchanged" value = "1" />  
  5.                <flag name = "stateHidden" value = "2" />  
  6.                <flag name = "stateAlwaysHidden" value = "3" />  
  7.         </attr>          
  8. </declare-styleable>  
11.多类型。
示例:
  1. <declare-styleable name = "名称">  
  2.        <attr name = "background" format = "reference|color" />  
  3. </declare-styleable>  

-------------------------------------------------------------------------------------------

二、属性的使用以及自定义控件的实现

1、构思控件的组成元素,思考所需自定义的属性。

       比如:我要做一个  <带阴影的按钮,按钮正下方有文字说明>(类似9宫格按钮) 

       新建values/attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="custom_view">  
  4.         <attr name="custom_id" format="integer" />  
  5.         <attr name="src" format="reference" />  
  6.         <attr name="background" format="reference" />  
  7.         <attr name="text" format="string" />  
  8.         <attr name="textColor" format="color" />  
  9.         <attr name="textSize" format="dimension" />  
  10.     </declare-styleable>  
  11. </resources>  

       以上,所定义为custom_view,custom_id为按钮id,src为按钮,background为阴影背景,text为按钮说明,textColor为字体颜色,textSize为字体大小。

2、怎么自定义控件呢,怎么使用这些属性呢?话不多说请看代码,CustomView :

  1. package com.nanlus.custom;  
  2.   
  3. import com.nanlus.custom.R;  
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Color;  
  7. import android.graphics.drawable.Drawable;  
  8. import android.util.AttributeSet;  
  9. import android.view.Gravity;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.FrameLayout;  
  13. import android.widget.ImageButton;  
  14. import android.widget.ImageView;  
  15. import android.widget.TextView;  
  16. public class CustomView extends FrameLayout implements OnClickListener {  
  17.   
  18.     private CustomListener customListener = null;  
  19.   
  20.     private Drawable mSrc = null, mBackground = null;  
  21.     private String mText = "";  
  22.     private int mTextColor = 0;  
  23.     private float mTextSize = 20;  
  24.     private int mCustomId = 0;  
  25.   
  26.     private ImageView mBackgroundView = null;  
  27.     private ImageButton mButtonView = null;  
  28.     private TextView mTextView = null;  
  29.   
  30.     private LayoutParams mParams = null;  
  31.   
  32.     public CustomView(Context context) {  
  33.         super(context);  
  34.   
  35.     }  
  36.   
  37.     public CustomView(Context context, AttributeSet attrs) {  
  38.         super(context, attrs);  
  39.   
  40.         TypedArray a = context.obtainStyledAttributes(attrs,  
  41.                 R.styleable.custom_view);  
  42.   
  43.         mSrc = a.getDrawable(R.styleable.custom_view_src);  
  44.         mBackground = a.getDrawable(R.styleable.custom_view_background);  
  45.         mText = a.getString(R.styleable.custom_view_text);  
  46.         mTextColor = a.getColor(R.styleable.custom_view_textColor,  
  47.                 Color.WHITE);  
  48.         mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);  
  49.         mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);  
  50.   
  51.         mTextView = new TextView(context);  
  52.         mTextView.setTextSize(mTextSize);  
  53.         mTextView.setTextColor(mTextColor);  
  54.         mTextView.setText(mText);  
  55.         mTextView.setGravity(Gravity.CENTER);  
  56.         mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,  
  57.                 LayoutParams.WRAP_CONTENT));  
  58.   
  59.         mButtonView = new ImageButton(context);  
  60.         mButtonView.setImageDrawable(mSrc);  
  61.         mButtonView.setBackgroundDrawable(null);  
  62.         mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,  
  63.                 LayoutParams.WRAP_CONTENT));  
  64.         mButtonView.setOnClickListener(this);  
  65.   
  66.         mBackgroundView = new ImageView(context);  
  67.         mBackgroundView.setImageDrawable(mBackground);  
  68.         mBackgroundView.setLayoutParams(new LayoutParams(  
  69.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  70.   
  71.         addView(mBackgroundView);  
  72.         addView(mButtonView);  
  73.         addView(mTextView);  
  74.   
  75.         this.setOnClickListener(this);  
  76.   
  77.         a.recycle();  
  78.     }  
  79.   
  80.     @Override  
  81.     protected void onAttachedToWindow() {  
  82.         super.onAttachedToWindow();  
  83.   
  84.         mParams = (LayoutParams) mButtonView.getLayoutParams();  
  85.         if (mParams != null) {  
  86.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;  
  87.             mButtonView.setLayoutParams(mParams);  
  88.         }  
  89.   
  90.         mParams = (LayoutParams) mBackgroundView.getLayoutParams();  
  91.         if (mParams != null) {  
  92.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;  
  93.             mBackgroundView.setLayoutParams(mParams);  
  94.         }  
  95.   
  96.         mParams = (LayoutParams) mTextView.getLayoutParams();  
  97.         if (mParams != null) {  
  98.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;  
  99.             mTextView.setLayoutParams(mParams);  
  100.         }  
  101.     }  
  102.   
  103.     public void setCustomListener(CustomListener l) {  
  104.         customListener = l;  
  105.     }  
  106.   
  107.     @Override  
  108.     public void onClick(View v) {  
  109.         if (customListener != null) {  
  110.             customListener.onCuscomClick(v, mCustomId);  
  111.         }  
  112.     }  
  113.   
  114.     public interface CustomListener {  
  115.         void onCuscomClick(View v, int custom_id);  
  116.     }  
  117. }  
代码很简单,就不多说,下面来看看我们的CustomView是怎么用的,请看:

 
3、自定义控件的使用

      话不多说,请看代码,main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.          android:layout_centerVertical="true"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <com.nanlus.custom.CustomView  
  15.             android:id="@+id/custom1"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_weight="1"  
  19.             nanlus:background="@drawable/background"  
  20.             nanlus:custom_id="1"  
  21.             nanlus:src="@drawable/style_button"  
  22.             nanlus:text="按钮1" >  
  23.         </com.nanlus.custom.CustomView>  
  24.     </LinearLayout>  
  25.   
  26.   
  27. </RelativeLayout>  

在这里需要解释一下,

xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"

nanlus为在xml中的前缀,com.nanlus.custom为包名

4、在Activity中,直接上代码

  1. package com.nanlus.custom;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.ImageButton;  
  6. import android.widget.ImageView;  
  7. import android.widget.TextView;  
  8. import android.widget.Toast;  
  9. import com.nanlus.BaseActivity;  
  10. import com.nanlus.custom.R;  
  11. import com.nanlus.custom.CustomView.CustomListener;  
  12.   
  13. public class CustomActivity extends BaseActivity implements CustomListener {  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.   
  19.         setContentView(R.layout.main);  
  20.   
  21.         ((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onCuscomClick(View v, int custom_id) {  
  26.         switch (custom_id) {  
  27.         case 1:  
  28.             Toast.makeText(this"hello !!!", Toast.LENGTH_LONG).show();  
  29.             break;  
  30.         default:  
  31.             break;  
  32.         }  
  33.   
  34.     }  
  35.   
  36. }  

至此,自定义控件属性以及运用讲解完毕,谢谢大家。本文链接: http://blog.csdn.net/nanlus/article/details/8219868


2
0
我的同类文章
猜你在找
Qt基础与Qt on Android入门
Android入门实战教程
Android底层技术:Java层系统服务(Android Service)
精讲精练_参悟Android核心技术
Android开源项目实践之UI篇
查看评论

  暂无评论

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
  • 个人资料
    • 访问:103816次
    • 积分:1111
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:8篇
    • 译文:0篇
    • 评论:23条
  • 文章分类
  • 最新评论


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值