加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

(转)attrs.xml文件的属性解释

发布时间:2020-12-16 02:09:28 所属栏目:百科 来源:网络整理
导读:转载自:http://www.jb51.cc/article/p-dbjgrucp-qy.html Android中在values中定义一个attrs.xml,然后自己定义一个组件MyView attrs.xml内容如下: [html] view plain copy ? xml version = "1.0" encoding = "utf-8" ? resources declare-styleable name =

转载自:http://www.52php.cn/article/p-dbjgrucp-qy.html


Android中在values中定义一个attrs.xml,然后自己定义一个组件MyView

attrs.xml内容如下:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. declare-styleablename="MyView">
  4. attrname="textColor"format="color"/>
  5. attrname="textSize"format="dimension"/>
  6. </declare-styleable>

定义的组件MyView:

copy

packagecom.jiangwei.demo;
  • importandroid.content.Context;
  • importandroid.content.res.TypedArray;
  • importandroid.graphics.Canvas;
  • importandroid.graphics.Color;
  • importandroid.graphics.Paint;
  • importandroid.graphics.Paint.Style;
  • importandroid.graphics.Rect;
  • importandroid.util.AttributeSet;
  • importandroid.view.View;
  • publicclassMyViewextendsView{
  • privatePaintmPaint;
  • privatestaticfinalStringmString="WelcometoMrWei'sblog";
  • publicMyView(Contextcontext){
  • super(context);
  • mPaint=newPaint();
  • }
  • publicMyView(Contextcontext,AttributeSetattrs){
  • super(context,attrs);
  • mPaint=newPaint();
  • TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.MyView);
  • inttextColor=a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);
  • floattextSize=a.getDimension(R.styleable.MyView_textSize,36);
  • mPaint.setTextSize(textSize);
  • mPaint.setColor(textColor);
  • a.recycle();
  • }
  • @Override
  • protectedvoidonDraw(Canvascanvas){
  • //TODOAuto-generatedmethodstub
  • super.onDraw(canvas);
  • //设置填充
  • mPaint.setStyle(Style.FILL);
  • //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  • canvas.drawRect(newRect(10,10,100,100),mPaint);
  • mPaint.setColor(Color.BLUE);
  • //绘制文字
  • canvas.drawText(mString,110,mPaint);
  • }

  • main.xml内容:

    copy

    LinearLayout
  • xmlns:android="http://schemas.android.com/apk/res/android"
  • xmlns:test="http://schemas.android.com/apk/res/com.jiangwei.demo"//一定记得添加前缀
  • android:layout_width="fill_parent"
  • android:layout_height="fill_parent"
  • android:orientation="vertical"com.jiangwei.demo.MyView
  • test:textSize="20px"//test是个前缀
  • test:textColor="#ffffff"LinearLayout>

  • 具体内容:

    格式如上,其中“xmlns:wen”冒号后面是标签名,在下面使用时(只对当前文件可用)
    <TextView wen:属性名/>
    “com.iteye.googlers”是你的工程包名。
    1. reference:参考某一资源ID。
    (1)属性定义:

    copy
    declare-styleablename="名称"attrname="background"format="reference" (2)属性使用:
    copy
    ImageView
  • android:layout_width="42dip"
  • android:layout_height="42dip"
  • android:background="@drawable/图片ID"
  • />

  • 2. color:颜色值。
    (1)属性定义:

    copy
    copy
    TextView
  • android:textColor="#00FF00"
  • />

  • 3. boolean:布尔值。
    (1)属性定义:
    copy
    attrname="focusable"format="boolean"copy
    Button
  • android:focusable="true"4. dimension:尺寸值。
    copy
    attrname="layout_width"format="dimension"copy
    5. float:浮点值。
    copy
    declare-styleablename="AlphaAnimation"attrname="fromAlpha"format="float"attrname="toAlpha"format="float"copy
    alpha
  • android:fromAlpha="1.0"
  • android:toAlpha="0.7"
  • />

  • 6. integer:整型值。
    copy
    declare-styleablename="AnimatedRotateDrawable"attrname="visible"attrname="frameDuration"format="integer"attrname="framesCount"format="integer"attrname="pivotX"attrname="pivotY"attrname="drawable"copy
    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:字符串。
    copy
    declare-styleablename="MapView"attrname="apiKey"format="string"copy
    com.google.android.maps.MapView
  • android:layout_width="fill_parent"
  • android:layout_height="fill_parent"
  • android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
  • 8. fraction:百分数。
    copy
    declare-styleablename="RotateDrawable"attrname="fromDegrees"format="float"attrname="toDegrees"format="float"attrname="pivotX"format="fraction"attrname="pivotY"format="fraction"copy
    rotate
  •   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:枚举值。
    copy
    attrname="orientation"enumname="horizontal"value="0"enumname="vertical"value="1"attr (2)属性使用:

    copy

    LinearLayout
  • android:orientation="vertical"
  • 10. flag:位或运算。
    (1)属性定义:

    copy

    attrname="windowSoftInputMode"flagname="stateUnspecified"value="0"flagname="stateUnchanged"value="1"flagname="stateHidden"value="2"flagname="stateAlwaysHidden"value="3"flagname="stateVisible"value="4"flagname="stateAlwaysVisible"value="5"flagname="adjustUnspecified"value="0x00"flagname="adjustResize"value="0x10"flagname="adjustPan"value="0x20"flagname="adjustNothing"value="0x30" lt;/declare-styleablecopy
    activity
  • android:name=".StyleAndThemeActivity"
  • android:label="@string/app_name"
  • android:windowSoftInputMode="stateUnspecified|stateUnchanged | stateHidden"intent-filteractionandroid:name="android.intent.action.MAIN"categoryandroid:name="android.intent.category.LAUNCHER"activity
    注意:

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

    copy
    attrname="background"format="reference|color"copy
    android:background="@drawable/图片ID|#00FF00"
  • />
  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读