首页 > 移动平台 > 详细

Android读取自定义View属性

时间:2014-10-10 10:49:45      阅读:399      评论:0      收藏:0      [点我收藏+]

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();
        }
      }

  }

Android读取自定义View属性

原文:http://www.cnblogs.com/l2rf/p/4015222.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!