android给自定义view添加XML属性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shb2058/article/details/81327158

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>

猜你喜欢

转载自blog.csdn.net/shb2058/article/details/81327158