Android读取自定义View属性

attrs.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyView">
        <attr name="MyViewColor" format="color"/>
    </declare-styleable>

</resources>

activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:background="@color/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.my.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fab:MyViewColor="@color/pink"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"/>
</RelativeLayout>

MyView.java :

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs, defStyle);

    if (attrs!= null) {
      TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.MyView, 0, 0);
      if (attr != null) {
        try {
          mMyViewColor = attr.getColor(R.styleable.MyViewColor, getColor(android.R.color.white));

        } finally {
          attr.recycle();
        }
      }

  }
时间: 2024-08-16 01:39:28

Android读取自定义View属性的相关文章

android 自定义View属性

在android开发过程中,用到系统的View时候可以通过XML来定义一些View的属性.比如ImageView: android:src  和android:scaleType为ImageView指定了图片源和图片缩放类型. 其实我们也可以自定义图片的这种属性. 下面以自定义标题栏为例,简单说明下自定义View属性. 比如在项目中,经常会用到标题栏,左边是返回,中间是标题,右边是下一步.如下图: 如果,每一次用到标题都在XML里面进行布局,那就太麻烦了.我们可以自定义一个标题栏. 自定义Vie

【android自定义控件】自定义View属性

1.自定义View的属性 2.在View的构造方法中获得我们自定义的属性 3.重写onMesure 4.重写onDraw 3这个步骤不是必须,当然了大部分情况下还是需要重写的. 1.自定义View的属性,首先在res/values/  下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式. <?xml version="1.0" encoding="utf-8"?> <resources> <attr name=&

UI--从学习styleable自定义view属性到一点儿更有意思的尝试

<代码里的世界> -UI篇 用文字札记描绘自己 android学习之路 转载请保留出处 by Qiao http://blog.csdn.net/qiaoidea/article/details/45599593 [导航] - 多行文本折叠展开 自定义布局View实现多行文本折叠和展开 1.概述 前面封装view的时候用到了自定义属性,觉得有必要单独讲一下这部分,但是呢,又不想向其他文章一样千篇一律地写这些东西.所以呢,后便会加一些临时的发散思维,引用点有意思的东西.分享东西嘛,随性点儿. 回

Android中自定义View的MeasureSpec使用

有时,Android系统控件无法满足我们的需求,因此有必要自定义View.具体方法参见官方开发文档:http://developer.android.com/guide/topics/ui/custom-components.html 一般来说,自定义控件都会去重写View的onMeasure方法,因为该方法指定该控件在屏幕上的大小. protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) onMeasure传

android中自定义view涉及到的绘制知识

android中自定义view的过程中,需要了解的绘制知识. 1.画笔paint: 画笔设置: <span style="font-size:14px;"> paint.setAntiAlias(true);//抗锯齿功能 paint.setColor(Color.RED); //设置画笔颜色 paint.setStyle(Style.FILL);//设置填充样式 paint.setStrokeWidth(30);//设置画笔宽度 paint.setShadowLayer(

Android 创建自定义 View 的属性 (attrs) 时需要注意的问题

自定义 View 的属性并不难,可以参照官方的文档 https://developer.android.com/training/custom-views/create-view.html 但是需要注意一个问题,否则可能浪费很多时间. <resources> <declare-styleable name="AppsControllerBlock"> <attr name="letterCase" format="enum&q

android 自定义view+属性动画实现充电进度条

近期项目中需要使用到一种类似手机电池充电进度的动画效果,以前没学属性动画的时候,是用图片+定时器的方式来完成的,最近一直在学习动画这一块,再加上复习一下自定义view的相关知识点,所以打算用属性动画和自定义view的方式来完成这个功能,将它开源出来,供有需要的人了解一下相关的内容. 本次实现的功能类似下面的效果: 接下来便详细解析一下如何完成这个功能,了解其中的原理,这样就能举一反三,实现其他类似的动画效果了. 详细代码请看大屏幕 https://github.com/crazyandcoder

Android自定义 view属性

第一种 /MainActivity/res/values/attrs.xml   <?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyCircle"> <attr name="radius" format="integer"/> <attr name=&

android自定义view属性

方法一: MyView.class package com.bwie.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class MyView ext