zoukankan      html  css  js  c++  java
  • android 自定义属性详解

    今天用到了自定义控件,也想用一下自定义属性,从网上搜了一下资料汇总的一下。

    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"

                         />

    2. 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"

                         />

    3. 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"

                        />

    4. dimension:尺寸值。

        (1)属性定义:

                <declare-styleable name = "名称">

                       <attr name = "layout_width" format = "dimension" />

                </declare-styleable>

        (2)属性使用:

                <Button

                        android:layout_width = "42dip"
                         android:layout_height = "42dip"

                        />

    5. 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"

                       />

    6. 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"

                       />

    7. 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"

                        />

    8. 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"

                       />

    9. 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>

    10. 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>

         注意:

         属性定义时可以指定多种类型值。

        (1)属性定义:

                <declare-styleable name = "名称">

                       <attr name = "background" format = "reference|color" />

                </declare-styleable>

        (2)属性使用:

                 <ImageView

                         android:layout_width = "42dip"
                          android:layout_height = "42dip"
                          android:background = "@drawable/图片ID|#00FF00"

                         />

     在xml 文件里定义控件的属性,我们已经习惯了android:attrs="" ,那么我们能不能定义自己的属性能,比如:test:attrs="" 呢?答案是肯定的.

            好了我就不卖关子了,直接进入主题。大致以下步骤:
            一、res/values 文件下定义一个attrs.xml 文件.代码如下:

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
    <declare-styleable name="MyView"> 
    <attr name="textColor" format="color" /> 
    <attr name="textSize" format="dimension" /> 
    </declare-styleable> 
    </resources> 

    二、 我们在MyView.java 代码修改如下,其中下面的构造方法是重点,我们获取定义的属性我们R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!

            获取,MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的名字,获取里面属性用名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!

     1 public MyView(Context context,AttributeSet attrs) 
     2 { 
     3 super(context,attrs); 
     4 mPaint = new Paint(); 
     5 TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView); 
     6 int textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF); 
     7 float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 
     8 mPaint.setTextSize(textSize); 
     9 mPaint.setColor(textColor); 
    10 a.recycle(); 
    11 } 

    MyView.java 全部代码如下:

     1 package eoe.demo; 
     2 
     3 
     4 import android.content.Context; 
     5 import android.content.res.TypedArray; 
     6 import android.graphics.Canvas; 
     7 import android.graphics.Color; 
     8 import android.graphics.Paint; 
     9 import android.graphics.Rect; 
    10 import android.graphics.Paint.Style; 
    11 import android.util.AttributeSet; 
    12 import android.view.View; 
    13 
    14 
    15 public class MyView extends View { 
    16 private Paint mPaint; 
    17 private Context mContext; 
    18 private static final String mString = "Welcome to Mr Wei's blog"; 
    19 public MyView(Context context) { 
    20 super(context); 
    21 mPaint = new Paint(); 
    22 } 
    23 public MyView(Context context,AttributeSet attrs) 
    24 { 
    25 super(context,attrs); 
    26 mPaint = new Paint(); 
    27 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); 
    28 int textColor = a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF); 
    29 float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 
    30 mPaint.setTextSize(textSize); 
    31 mPaint.setColor(textColor); 
    32 a.recycle(); 
    33 } 
    34 
    35 
    36 @Override 
    37 protected void onDraw(Canvas canvas) { 
    38 // TODO Auto-generated method stub 
    39 super.onDraw(canvas); 
    40 //设置填充 
    41 mPaint.setStyle(Style.FILL); 
    42 //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标 
    43 canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); 
    44 mPaint.setColor(Color.BLUE); 
    45 //绘制文字 
    46 canvas.drawText(mString, 10, 110, mPaint); 
    47 } 
    48 } 

       将我们自定义的MyView 加入布局main.xml 文件中,平且使用自定义属性,自定义属性必须加上:
    xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor "蓝色 是自定义属性的前缀,红色是我们包名.

     1 <?xml version="1.0" encoding="utf-8"?> 
     2 <LinearLayout 
     3 xmlns:android="http://schemas.android.com/apk/res/android" 
     4 
     5 xmlns:test="http://schemas.android.com/apk/res/com.android.tutor" 
     6 android:orientation="vertical" 
     7 android:layout_width="fill_parent" 
     8 android:layout_height="fill_parent" 
     9 > 
    10 
    11 
    12 <TextView 
    13 android:layout_width="fill_parent" 
    14 android:layout_height="wrap_content" 
    15 android:text="@string/hello" 
    16 /> 
    17 
    18 
    19 <com.android.tutor.MyView 
    20 android:layout_width="fill_parent" 
    21 android:layout_height="fill_parent" 
    22 test:textSize="20px" 
    23 test:textColor="#fff" 
    24 /> 
    25 </LinearLayout>
  • 相关阅读:
    Win7 64位系统上Hadoop单机模式的安装及开发环境搭建
    HBase配置文件设置
    YARN HA 配置文件设置
    Hadoop的配置文件设置(HDFS HA)
    记一次java heap space的解决办法
    记一次sql优化——left join不走索引问题
    js黑魔法
    css坑了我一下下之line-height
    target-densitydpi=device-dpi会使其他ui插件布局变小
    redis缓存过期key优化-缓存不释放
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/2855803.html
Copyright © 2011-2022 走看看