当前位置:操作系统 > 安卓/Android >>

Android中自定义组件及自定义属性

有时候在做开发的时候,android提供给我们的视图并不能满足我们的要求,所以有时候我们需要自己创建自己的view。
我们只需要将我们想要的继承于View,然后重写里面的方法就可以了。
[java]  
package com.example.view;  
  
import android.content.Context;  
import android.graphics.Color;  
import android.util.AttributeSet;  
import android.widget.TextView;  
  
public class MyTextView extends TextView {  
  
    public MyTextView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        this.setTextColor(Color.BLUE);// 将字体设置成蓝色  
    }  
  
}  
 
然后我们只需要在Layout中使用这个view就可以了。
[html]  
<?xml version="1.0" encoding="UTF-8"?>  
<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" >  
  
    <com.example.myviewtest01  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
        android:layout_centerVertical="true"  
        android:text="@string/hello_world" />  
  
</RelativeLayout>  
[html]  
<p><span style="font-size:14px;">如果我们要自定义属性,就像<span style="color:#7f07f;">android:layout_height</span><span style="color:black;">=</span><em><span style="color:#2a0ff;">"wrap_content"</span></em>这种。</span></p><p><span style="font-family:Calibri;font-size:14px;"> </span></p><p><span style="font-size:14px;">首先我们要学习</span><span style="color:#4b4b4b;">declare-styleable,</span><span style="color:#4b4b4b;">它是给自定义控件添加自定义属性时用的。</span></p><p><span style="color:#4b4b4b;">我们在</span><span style="color:#4b4b4b;">res/values</span><span style="color:#4b4b4b;">下建立一个</span><span style="color:#4b4b4b;">myAttrs.xml</span></p>  
[html]  
<pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
<resources>  
  
    <declare-styleable name="MyTextView">  
        <attr name="fontSize" format="dimension" />  
    </declare-styleable>  
  
</resources>  
</pre><br>  
<p><span style="color:teal">解释一下上面那些值的属性</span></p>  
<p><span style="color:teal">Name=”MyTextView” </span><span style="color:teal">是在</span><span style="color:teal">R.java</span><span style="color:teal">文件生成对应的引用名。</span></p>  
<p align="left"><span style="color:teal"><</span><span style="color:#3f7f7f">attr</span>  
<span style="color:#7f07f">name</span><span style="color:black">=</span><em><span style="color:#2a0ff">"fontSize"</span></em><span style="color:#7f07f">format</span><span style="color:black">=</span><em><span style="color:#2a0ff">"dimension"</span></em>  
<span style="color:teal">/></span><span style="color:teal">这个就是自定义的属性名称。在</span><span style="color:teal">R.java</span><span style="color:teal">中会生成对应的名字,这个地方是</span><span style="color:teal">MyTextView_fontSize.</span></p>  
<p align="left"><span style="color:teal">后面的</span><span style="color:teal">format</span><span style="color:teal">是定义的你的变量的属性。如果你想学习有关他的更多详细信息,请转向</span><span style="color:teal"><a href="http://blog.csdn.net/lihengfang/article/details/8290754"><span style="color:#0563c1">http://blog.csdn.net/lihengfang/article/details/8290754</span></a></span></p>  
<p align="left"><span style="color:teal"> </span></p>  
<p align="left"><span style="color:teal"> </span></p>  
<p align="left"><span style="color:teal">在布局文件中我们就可以直接这样写</span></p>  
<pre></pre>  
<pre class="html" name="code"><pre class="html" name="code"><?xml version="1.0" encoding="UTF-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    xmlns:mytextview="http://schemas.android.com/apk/res/com.example.myviewtest"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
  
    <com.example.myviewtest.MyTextView  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        mytextview:fontSize="20dp"  
        android:text="text size = 20dp" >  
    </com.example.myviewtest.MyTextView>  
    <com.example.myviewtest.MyTextView  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        mytextview:fontSize="15dp"  
        android:text="text size=15dp" >  
    </com.example.myviewtest.MyTextView>  
  
</LinearLayout>  
  
</p
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,