1. 程式人生 > >android給自定義view新增XML屬性

android給自定義view新增XML屬性

1.在value下新建檔案(檔名隨便),把需要的名稱和型別放進去

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RefreshRecycleview">
        <attr name="isNeedLoadMore" format="boolean"/>
    </declare-styleable>
</resources>

2.在view的構造方法中取出來

  TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.RefreshRecycleview);
        needLoadMore=typedArray.getBoolean(R.styleable.RefreshRecycleview_isNeedLoadMore,true);

3.在xml中使用,這裡如果XML已經有

xmlns:app="http://schemas.android.com/apk/res-auto",可以直接使用app:名稱,一樣的
    <com.dxxx.refreshloadmorerecycleview.recycleview.Refresh_Loadmore_Layout
        xmlns:refreshrecycleview="http://schemas.android.com/apk/res-auto"
        refreshrecycleview:isNeedLoadMore="false"
        android:id="@+id/refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></com.dxxx.refreshloadmorerecycleview.recycleview.Refresh_Loadmore_Layout>