Android控件自定义属性(declare-styleable属性详解)

Android控件自定义属性(declare-styleable属性详解)我们在做项目的时候,由于android自带的属性不能满足需求,android提供了自定义属性的方法,其中的format是做什么用的?以及如何使用它?下面列出一些常用的。1.reference:参考某一资源ID。   (1)属性定义:                                           (2)属性使用:

大家好,又见面了,我是你们的朋友全栈君。

在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义
控件时,难免要用到自定义属性,那怎么使用自定义属性呢?

一、简单使用:

1.在文件res/values/下新建attrs.xml属性文件,中定义我们所需要的属性:
<resources><!-- resource是跟标签,可以在里面定义若干个declare-styleable -->
<declare-styleable name="custom_view"><!-- name定义了变量的名称 -->
    <attr name="custom_color" format="color"></attr> <!-- 定义对应的属性,name定义了属性的名称 -->
    <attr name="custom_size" format="dimension"></attr> <!--每一个发生要定义format指定其类型,类型包括 
      reference   表示引用,参考某一资源ID
      string   表示字符串
      color   表示颜色值
      dimension   表示尺寸值
      boolean   表示布尔值
      integer   表示整型值
      float   表示浮点值
      fraction   表示百分数
      enum   表示枚举值
      flag   表示位运算
    -->
</declare-styleable>

2.在布局中使用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f6f6f6"
    android:orientation="vertical"
    android:padding="10dp" >

    <com.ldm.learn.CustomTextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="自定义TextView"
        ldm:custom_color="#333333"  ldm:custom_size="35sp" />

</LinearLayout>

2.在代码中引用:

public class CustomTextView extends TextView {
    private int textSize;//自定义文件大小
    private int textColor;//自定义文字颜色

    //自定义属性,会调用带两个参数的构造方法
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_view);//TypedArray属性对象  textSize = ta.getDimensionPixelSize(R.styleable.custom_view_custom_size, 20);//获取属性对象中对应的属性值  textColor = ta.getColor(R.styleable.custom_view_custom_color, 0x0000ff);
        setColorAndSize(textColor, textSize);//设置属性
        ta.recycle();
    }

    public CustomTextView(Context context) {
        super(context);
    }

    private void setColorAndSize(int textColor, int textSize) {
        setTextColor(textColor);
        setTextSize(textSize);
    }

}

二、declare-styleable属性详解:

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”

                     />

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158784.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Django(26)HttpResponse对象和JsonResponse对象「建议收藏」

    Django(26)HttpResponse对象和JsonResponse对象「建议收藏」HttpResponse对象Django服务器接收到客户端发送过来的请求后,会将提交上来的这些数据封装成一个HttpRequest对象传给视图函数。那么视图函数在处理完相关的逻辑后,也需要返回一个响

    2022年7月30日
    2
  • 深度学习#1.有监督学习和无监督学习

    深度学习#1.有监督学习和无监督学习机器学习#1.有监督学习和无监督学习人工智能与机器学习与深度学习机器学习有监督学习无监督学习人工智能与机器学习与深度学习什么是人工智能?人类用各种方式让机器拥有与人类相仿的“智能”,就是人工智能。什么是机器学习?机器学习就是实现人工智能的一种方式。什么是深度学习?深度学习就是实现机器学习的一种技术。机器学习为什么要机器学习呢?你想啊,人类这么聪明是为什么,还不是因为在整个人类历史…

    2022年5月27日
    45
  • 撸完之后

    撸完之后坚持了一周的加班,今天终于告一段落了,回家自己亲手做了一顿饭,吃完之后,开始无聊的无聊。

    2022年8月22日
    3
  • 小程序onLaunch事件的坑「建议收藏」

      记一个小程序踩过的坑小程序项目中app.js里面定义了globalData,即全局变量,里面定义了一个token字段需求是这样的,每次进入小程序的时候需要检验该token有没有,没有就请求后台获取token,由于我一开始将该检验函数A放在onLaunch事件里面,但是这个函数A是引入的其他js文件里面的,这时候我在这个js文件里面使用constapp=getApp()的时候发现获…

    2022年4月14日
    48
  • Tomcat中的ResourceBundle国际化解析「建议收藏」

    Tomcat中的ResourceBundle国际化解析「建议收藏」一、ResourceBundle简介:资源束(ResourceBundle)是一个本地化对象。它封装了适用于本地环境的资源;这个类主要用来解决国际化和本地化问题。国际化和本地化可不是两个概念,两者都是一起出现的。可以说,国际化的目的就是为了实现本地化。比如对于“取消”,中文中我们使用“取消”来表示,而英文中我们使用“cancel”。若我们的程序是面向国际的…

    2022年7月12日
    15
  • oracle隐式转换和显式转换_oracle显示游标和隐式

    oracle隐式转换和显式转换_oracle显示游标和隐式和其他的关系型数据库一样,oracle中也能进行一些隐式的数据转换,这对我们写SQL语句有非常用,我们可以不必麻烦地手动转化很多类型的字符。虽然前面我们介绍了一些使用例如to_char,to_date的函数进行强制转换的方法,但是隐式转换也还是不错的。Orac和其他的关系型数据库一样,oracle中也能进行一些隐式的数据转换,这对我们写SQL语句有非常用,我们可以不必麻烦地手动转化很…

    2022年10月11日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号