首页 > 代码库 > Android读取自定义View属性
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(); } } }
Android读取自定义View属性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。