android中怎么在View构造的attrs中拿到android给的属性以及attrs属性介绍[通俗易懂]

android中怎么在View构造的attrs中拿到android给的属性以及attrs属性介绍[通俗易懂]attrs获得,而自定义的属性获得值方式如下,当然原生的也是一样,只需要把attrname该成系统的。一、首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:在这里,需要补充attrs属性的相关知识,即Attr属性是如何在

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

attrs获得,而自定义的属性获得值方式如下,当然原生的也是一样,只需要把attr name该成系统的。

一、 首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="my_text_view">
       <attr name="text_size" format="float"></attr>
       <attr name="text_color" format="color"></attr>
       <attr name="text_back_ground" format="color|reference"></attr>

   </declare-styleable>
</resources>

在这里,需要补充attrs属性的相关知识,即Attr属性是如何在XML中定义的,自定义属性的Value值可以有10种类型以及其类型的组合值,其具体使用方法如下:

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

二、接下来实现自定义View的类,其中下面的构造方法是重点,在代码中获取自定义属性,其代码如下:

package com.example.CustomAttr;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

/**
 * Created with IntelliJ IDEA.
 * User: zhiwen.nan
 * Date: 13-9-28
 * Time: 下午10:00
 * To change this template use File | Settings | File Templates.
 */
public class CustomTextView extends TextView {
    private TypedArray mTypedArray;
    private Paint mPaint;

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

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inial(context,attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inial(context,attrs);
    }

    private void inial(Context context,AttributeSet attrs)  {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.my_text_view);

float textsize = typedArray.getFloat(R.styleable.my_text_view_text_size,14)   ;
        int textColor = typedArray.getColor(R.styleable.my_text_view_text_color,0xFFFFFF)   ;
        int bgColor = typedArray.getColor(R.styleable.my_text_view_text_back_ground,0xFFFFFF) ;

        super.setTextColor(textColor);
        super.setTextSize(textsize);
        super.setBackgroundColor(bgColor);

typedArray.recycle();

    }

}

三、接下来在XML布局中引用自定义View控件,其XML代码如下:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <com.example.CustomAttr.CustomTextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MainActivity"
            app:text_size ="20"
            app:text_color ="#00FF00"
            app:text_back_ground="#ffffff"

            />
</LinearLayout>

注意上面XML中代码:   xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr",是自定义的app命名空间,res后面是应用程序包名,然后可以直接使用app:text_size,等属性,其值类型要和attrs.xml定义的属性Value值相对应。
四、总结:
    注意该例子中是使用app:text_size = "20 和app:text_color="#00FF00定义TextView的颜色和textView的字体大小,而不是使用系统的属性android:textsize等。该例子中只是起到抛砖引玉的作用,你可以自定义其他属性,来实现你想要的自定义View效果。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

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


相关推荐

  • django的drf_简述django请求生命周期

    django的drf_简述django请求生命周期前言一般我们写完序列化以后,我们就会开始写视图了,drf中我们一般使用CBV的方式,也就是类视图的方式,最基础的我们会使用fromrest_framework.viewsimportAPIVi

    2022年7月31日
    2
  • 美股实时行情api接口(美股开户)

    所有美股历史交易行情数据,完整历史交易/任意时间段查询。1.产品功能支持所有历史美股交易查询;支持指定任意时间段查询;超高的查询效率,秒级返回;所有的交易数据为未复权的数据;全接口支持HTTPS(TLSv1.0/v1.1/v1.2/v1.3);全面兼容AppleATS;全国多节点CDN部署;接口极速响应,多台服务器构建API接口负载均衡。2.API文档接口地址:https://api.gugudata.com/stock/us返回格式:

    2022年4月11日
    135
  • Windows 7系统DNS服务器配置方法

    Windows 7系统DNS服务器配置方法DNSDNS域名系统。域名系统是一个有序、结构化系统用于计算机与互联网或连接在一个私密网络系统。每个参与者补充了一个域名,代表不同的信息。电脑系统要求数字IP地址的功能。然而,这显然是非常困难的,对一些IP地址的门外汉来说,DNS可以翻译处容易记住的域名,DNS的重要性在于它是一个目录服务的互联网。正如电话目录翻译的人的名字到他们的电话号码,DNS也是这样,它们会翻译IP地

    2022年5月22日
    31
  • traceroute详解[通俗易懂]

    traceroute详解[通俗易懂]traceroute详解1.traceroute基本概念traceroute(Windows系统下是tracert)命令利用ICMP协议定位您的计算机和目标计算机之间的所有路由器。TTL值可以反映数据包经过的路由器或网关的数量,通过操纵独立ICMP呼叫报文的TTL值和观察该报文被抛弃的返回信息,traceroute命令能够遍历到数据包传输路径上的所有路由器。traceroute是一条缓慢…

    2022年6月29日
    76
  • 一款好用的Linux系统服务器性能监控分析工具介绍「建议收藏」

    一款好用的Linux系统服务器性能监控分析工具介绍「建议收藏」软件性能测试过程中经常要对服务器性能指标(比如CPU、内存、磁盘IO及网络IO等等)进行监控以分析出软件在此服务器上的性能瓶颈以便进行后续的服务器调优及软件性能优化。下面为大家介绍一款小编认为比较好用的Linux系统服务器性能监控分析工具:nmonforLinux。从nmon工具包中选择监控服务器匹配的nmon监控可执行文件(如下图所示:小编使用的是nmon_linux_x86_64)将…

    2022年5月30日
    31
  • python 入门学习—模块导入三种方式及中文凝视

    python 入门学习—模块导入三种方式及中文凝视

    2022年1月20日
    53

发表回复

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

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