zoukankan      html  css  js  c++  java
  • Android读取自定义View属性

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

  • 相关阅读:
    0. 序列
    Megacli 常用
    4. Storm可靠性
    3. Storm编程框架
    2. Storm消息流
    1.1 Storm集群安装部署步骤
    poj3723,最 大 生成树
    次短路
    无间道之并查集
    最小生成树二Kruscal算法
  • 原文地址:https://www.cnblogs.com/l2rf/p/4015222.html
Copyright © 2011-2022 走看看