Android 属性使用

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="BannerViewArray">
    <attr name="startAndclose" format="boolean"></attr>
    <attr name="startTime" format="integer"></attr>
    </declare-styleable>

</resources>
public BannerViewArray(Context context, @Nullable AttributeSet attrs) {
       this(context,attrs,0);
        TypedArray ty = context.obtainStyledAttributes(attrs, R.styleable.BannerViewArray);
        startAndClose = ty.getBoolean(R.styleable.BannerViewArray_startAndclose, false);
        //获取时间值
        bannerTime = ty.getInteger(R.styleable.BannerViewArray_startTime,1000);
        ty.recycle();//释放
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:banner="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <com.example.app0928day03.BannerViewArray
       android:id="@+id/banner"
       banner:startAndclose="true"
       banner:startTime="3000"
       android:layout_width="match_parent"
       android:layout_height="200dp"/>

</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/qq_42234894/article/details/82961053
今日推荐