谈谈- declare-styleable属性

谈谈- declare-styleable属性在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义 控件时,难免要用到自定义属性,那怎么使用自定义属性呢?一、简单使用:2.在布局中使用:private

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

在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”

                     />

转载:http://blog.csdn.net/langxinlen/article/details/50343175

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

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

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


相关推荐

  • mysql全文索引是什么_Mysql中的全文索引

    以前只是简单听说过Mysql有全文索引,但是一直没有认真去了解过。最近在《MYSQL必知必会》中学习到这个知识点,做下记录。首先,什么是全文索引?简单来说,全文索引其实就是类似于LIKE语句,把包含一定的字符串的的行记录挑选出来。那么问题来了,既然只是达到这个需求的话使用LIKE就行了,LIKE不行的话也还能使用正则表达式,为什么还要大费周章弄个全文索引出来呢?书上提到了三个原因:①性能,Like…

    2022年4月9日
    78
  • this.class.getClassLoader()怎么理解?「建议收藏」

    this.class.getClassLoader()怎么理解?「建议收藏」this.class.getClassLoader()怎么理解?java是面向对象语言,面向对象的语言的宗旨就是万事万物皆对象,那么类也是一个对象,类里面的属性和方法也是对象。java里面的所有的类都是Class类的对象,这个this.class是获得这个类相对于Class类的对象。后面的方法是获得这个类对象的加载器。只有Class类才有getClassLoader()方法呀~可以这么想,我们平时讲述某某类,但是我们并没有说这个类怎么和虚拟机打交道,虚拟机怎么识别这个类.总不能全靠字符

    2022年6月1日
    35
  • 薅羊毛利器—Loon,Cookie放在本地一点也不担心[通俗易懂]

    LoonLoon是一款在iOS系统下的代理工具(目前还没有Android版本),它支持在本地执行js脚本,只需简单配置即可成为薅羊毛利器ps之前的羊毛脚本已经失效了下载可以去美区AppStore下载,价格$4.99,需要有一个美区的AppleId账号,并且充值美元可以去某宝或拼夕夕搜索并购买,价格大概在¥9.99配置然后将下面框里面的配置赋值粘贴进去,点击保存[General]#IPv6支持ipv6=false##skip-proxy和bypass-tun一般不需

    2022年4月14日
    153
  • Matlab中的plotyy用法总结[通俗易懂]

    Matlab中的plotyy用法总结[通俗易懂]在梳理数据,或者写论文的时候往往需要画出两个纵坐标不同的图,以显现两组数据的不同变化,在这种情况下,一般两组数据的横坐标均是相同的,但是纵坐标确实不是一个数量级的,所以为了更好的表现两组数据的变化趋势,需要用到plotyy函数绘制双纵坐标的二维图,具体用法总结如下:调用格式:1.plotyy(X1,Y1,X2,Y2):以左、右不同纵轴绘制X1-Y1、X2-Y2两条曲线。2.plotyy(…

    2022年6月29日
    61
  • jboss项目文件路径_java读取一个文件夹下的所有文件

    jboss项目文件路径_java读取一个文件夹下的所有文件==========转自:https://blog.csdn.net/tomcat_2014/article/details/50624120===========JavaWeb中读取文件资源的路径问题转载 2016年02月02日15:34:4916757在做javaweb开发的时候,我们可能会需要从本地硬盘上读取某一个文件资源,或者修改某一个文件,这个时候就需要先找到这个文件,然后用FileI…

    2026年1月22日
    3
  • Oculus Rift DK2 安装所需电脑配置「建议收藏」

    Oculus Rift DK2 安装所需电脑配置

    2022年1月25日
    50

发表回复

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

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