zoukankan      html  css  js  c++  java
  • android自定义View的用法

    一、统一的用户界面是可以使得应用程序更友好。要做到用户界面的统一,我们就必须用到风格(style)和主题(theme)。
    自定义一个View的方法步骤如下:
    1、首先,在values文件夹下定义一个atts.xml的文件,描述自定义的控件的属性
    在values/attrs.xml中:
    1. <?xml version="1.0" encoding="utf-8"?>  
    2.  <resources>  
    3.     <declare-styleable name="TestView">  
    4.         <attr name="textColor" format="color" />  
    5.         <attr name="textSize" format="dimension" />  
    6.         <attr name="imgBackground" format="integer" />  
    7.         <attr name="textPaddingLeft" format="dimension"/>  
    8.         <attr name="textPaddingTop" format="dimension"/>  
    9.     </declare-styleable>  
    10.  </resources>  
    2、其次,定义一个继承自View的类,如:TestView,使其实现View的方法
    1. package com.test.TestView;  
    2. import android.content.Context;  
    3. import android.content.res.TypedArray;  
    4. import android.graphics.Canvas;  
    5. import android.graphics.Color;  
    6. import android.graphics.Paint;  
    7. import android.util.AttributeSet;  
    8. import android.view.View;  
    9.   
    10. public class TestView extends View {    
    11.     private Paint mPaint;  
    12.     private Context mContext;  
    13.     private String mStr;  
    14.     public TestView(Context context, AttributeSet attrs) {//构造方法;根据需要实现继承自View的方法   
    15.         super(context, attrs);  
    16.         mContext = context;  
    17.         initMyView();  
    18.         //对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义。   
    19.         TypedArray params = context.obtainStyledAttributes(attrs,R.styleable.MyView);  
    20.         //得到自定义控件的属性值。   
    21.         int backgroudId = params.getResourceId(R.styleable.MyView_imgBackground, 0);  
    22.         if (backgroudId != 0)  
    23.             setBackgroundResource(backgroudId);  
    24.         int textColor = params.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);  
    25.         setTextColor(textColor);  
    26.         float textSize = params.getDimension(R.styleable.MyView_textSize, 36);  
    27.         setTextSize(textSize);  
    28.         float paddingLeft = params.getDimension( R.styleable.MyView_textPaddingLeft, 41);  
    29.         float paddingTop = params.getDimension(R.styleable.MyView_textPaddingTop, 21);  
    30.         setPaddings(paddingLeft, paddingTop);  
    31.     }  
    32.     @Override  
    33.     protected void onDraw(Canvas canvas) {  
    34.         super.onDraw(canvas);  
    35.         if (mStr != null) {  
    36.             canvas.drawText(mStr, 3060, mPaint);  
    37.         }  
    38.     }  
    39.     private void initMyView() {  
    40.         mPaint = new Paint();  
    41.         mPaint.setColor(Color.WHITE);  
    42.     }  
    43.     private void setTextColor(int textColor) {  
    44.         mPaint.setColor(0XFFAABBCC);  
    45.     }  
    46.     private void setTextSize(float textSize) {  
    47.         mPaint.setTextSize(textSize);  
    48.     }  
    49.     void setText(String text) {  
    50.         mStr = text;  
    51.     }  
    52.     private void setPaddings(float paddingLeft, float paddingTop) {  
    53.         setPadding((int) paddingLeft, (int) paddingTop, 00);  
    54.     }  
    55. }  
    3、然后,在Layout文件中应用该自定义的view,如下:
    如在main.xml中
    1. <?xml version="1.0" encoding="utf-8"?>  
    2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.                xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"    
    4.                android:orientation="vertical" android:layout_width="fill_parent"  
    5.                android:layout_height="fill_parent">  
    6.     <com.test.TestView.TestView  
    7.               android:id="@+id/myview"  
    8.               android:layout_width="fill_parent"  
    9.               android:layout_height="fill_parent"  
    10.               app:textColor="#FFFFFFFF"  
    11.               app:textSize="40dip"  
    12.               app:textPaddingLeft="40dip"  
    13.               app:textPaddingTop="40dip"  
    14.               app:imgBackground="@drawable/bg" />  
    15.  </LinearLayout>  
    说明:上述红色部分——xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"是首先声明命名空间。命名空间为app.路径是http://schemas.android.com/apk/res/这一部分是不变的,后面接的是R的路径:com.test.TestView.R。然后在自定义控件的xml描述中就可以这样使用app:value="true"。这样就实现了自定义控件的初始化赋值。
    4、然后就是使用了,在自己的Activity 中
    1. public class TestActivity extends Activity {  
    2.      
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         setContentView(R.layout.main);  
    7.          
    8.         TestView mTextView=(TestView)findViewById(R.id.TestView);  
    9.         mTextView.setText("自定义的View");  
    10.     }  
    11. }  

    5、另外:

    以下内容转自:http://www.android123.com.cn/androidkaifa/591.html

        对于Android系统的自定义View可能大家都熟悉了,对于自定义View的属性添加,以及Android的Layout的命名空间问题,很多网友还不是很清楚,今天Android123一起再带大家温习一下

    1. CwjView myView=new CwjView(context);  

          

      如果用于游戏或整个窗体的界面,我们可能直接在onCreate中setContentView(myView); 当然如果是控件,我们可能会需要从Layout的xml中声明,比如

    1. <cn.com.android123.CwjView  
    2.        android:layout_width="wrap_content"  
    3.        android:layout_height="wrap_content"  
    4. />  

      当然,我们也可以直接从父类声明比如

    1. <View class="cn.com.android123.CwjView"  
    2.       android:layout_width="wrap_content"  
    3.       android:layout_height="wrap_content"  
    4. />  

       上面我们仅用了父类View的两个属性,均来自android命名空间,而名称为layout_width或layout_height,我们自定义的控件可能有更多的功能,比如

    1. <cn.com.android123.CwjView   
    2.       android:layout_width="wrap_content"  
    3.       android:layout_height="wrap_content"  
    4.      cwj:age="22"  
    5.      cwj:university="sjtu"  
    6.      cwj:city="shanghai"  
    7. />  

     我们可以看到上面的三个属性,是我们自定义的。作为标准xml规范,可能还包含了类似 xmlns:android="http://schemas.android.com/apk/res/android"  这样的语句,对于定义完整的View,我们的命名空间为cwj,这里可以写为  

           xmlns:cwj=http://schemas.android.com/apk/res/cn.com.android123.cwjView 或        

           xmlns:cwj=http://schemas.android.com/apk/res/android 都可以

      对于定义的cwj命名空间和age、university以及city的三个属性我们如何定义呢? 在工程的res/values目录中我们新建一个cwj_attr.xml文件,编码方式为utf-8是一个好习惯,内容如下

    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <resources>  
    3.     <declare-styleable name="CwjView">  
    4.        <attr name="age" format="integer" />  
    5.        <attr name="city" format="string" />  
    6.        <attr name="university" format="string" />  
    7.    </declare-styleable>  
    8. </resources>  

      这里我们可能对format不是很熟悉,目前Android系统内置的格式类型有integer比如ProgressBar的进度值,float比如RatingBar的值可能是3.5颗星,boolean比如ToggleButton的是否勾选,string比如TextView的text属性,当然除了我们常见的基础类型外,Android的属性还有特殊的比如color是用于颜色属性的,可以识别为#FF0000等类型,当然还有dimension的尺寸类型,比如23dip,15px,18sp的长度单位,还有一种特殊的为reference,一般用于引用@+id/cwj @drawable/xxx这样的类型。

      当然什么时候用reference呢? 我们就以定义一个颜色为例子:

      <attr name="red" format="color|reference" />  这里我们用了逻辑或的运算符,定义的红色是颜色类型的,同时可以被引用。当然,对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义。在我们自定义View的构造方法(Context context, AttributeSet attrs)的重载类型中可以用

    1. public CwjView(Context context, AttributeSet attrs) {  
    2.     super(context, attrs);  
    3.     TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.cwj_attr);  
    4.     mAge = a.getInteger(R.styleable.CwjView_age, 22);  
    5.     mCity = a.getString(R.styleable.CwjView_city, "shanghai");  
    6.     mUniversity= a.getString(R.styleable.CwjView_university, "sjtu");          
    7.     a.recycle(); //Android123提示大家不要忘了回收资源   
    8.   
    9. }  

    这样类的全局成员变量 mAge、mCity就获取了我们需要的内容,当然根据layout中的数值我们自定义的CwjView需要动态的处理一些数据的情况,可以使用AttributeSet类的getAttributeResourceValue方法获取。

    1. public CwjView(Context context, AttributeSet attrs){  
    2.     super(context, attrs);  
    3.     resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView""age"100);    
    4.     resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView""city""shanghai");  
    5.     //resID就可以任意使用了   

    以上两种方法中,参数的最后一个数值为默认的,如果您有不明白的地方可以来函到 android123@163.com 我们会在第一时间回复。

  • 相关阅读:
    20
    19下
    19上
    18下
    20145218 《Java程序设计》第五周学习总结
    20145218 《Java程序设计》第四周学习总结
    20145218 《Java程序设计》第三周学习总结
    20145218 《Java程序设计》第二周学习总结
    20145218《Java程序设计》第一周学习总结
    问卷调查
  • 原文地址:https://www.cnblogs.com/shuiyun/p/2742740.html
Copyright © 2011-2022 走看看