自定义提示通知条

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

上图:

代码:

/**
 * 类描述:界面通知
 */
class NoticeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : LinearLayout(context, attrs, defStyleAttr), View.OnClickListener {
    private var tv_notice: TextView?=null
    private var iv_alarm: ImageView?=null
    private var iv_close: ImageView?=null

    init {
        initView(context)
    }

    private fun initView(context: Context) {
        //初始化布局参数
        alpha = 0.85f
        gravity = Gravity.CENTER_VERTICAL
        val padingValue = ScreenUtil.dip2px(context, 10f)
        setPadding(padingValue, padingValue, padingValue, padingValue)
        background = resources.getDrawable(R.drawable.shape_notice_bg)
        //闹钟
        iv_alarm = ImageView(context)
        iv_alarm!!.setImageResource(R.drawable.ic_access_alarm_black_24dp)
        this.addView(iv_alarm)
        //通知
        tv_notice = TextView(context)
        tv_notice!!.gravity = Gravity.CENTER_HORIZONTAL
        tv_notice!!.layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
        this.addView(tv_notice)
        //关闭通知
        iv_close = ImageView(context)
        iv_close!!.setImageResource(R.drawable.ic_clear_black_24dp)
        this.addView(iv_close)
        //点击监听
        iv_close!!.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        if (view == iv_close) {
            this.visibility = View.GONE
        }
    }

    /**
     * 显示设置的文字
     * @param msg
     */
    fun setNoticeMsgAndShow(msg: String) {
        if (!TextUtils.isEmpty(msg)) {
            this.tv_notice!!.text = msg
            this.visibility = View.VISIBLE
        }

    }

    fun getIvAlarm(): ImageView? {
       return this.iv_alarm
    }

    fun getIvClose() : ImageView?{
        return this.iv_close
    }
}

XML文件:

<com.ming.demo.android.p.view.NoticeView
        android:layout_marginTop="50dp"
        android:layout_margin="10dp"
        android:id="@+id/notice_view"
        android:layout_gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

</com.ming.demo.android.p.view.NoticeView>

资源文件shape_notice_bg:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="#FFFFFF"/>
    <stroke
        android:width="1dp"
        android:color="#f79640"/>
</shape>

资源文件ic_access_alarm_black_24dp:

<vector android:height="24dp" android:tint="#0D0B81"
    android:viewportHeight="24.0" android:viewportWidth="24.0"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M22,5.72l-4.6,-3.86 -1.29,1.53 4.6,3.86L22,5.72zM7.88,3.39L6.6,1.86 2,5.71l1.29,1.53 4.59,-3.85zM12.5,8L11,8v6l4.75,2.85 0.75,-1.23 -4,-2.37L12.5,8zM12,4c-4.97,0 -9,4.03 -9,9s4.02,9 9,9c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,20c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z"/>
</vector>

 资源文件ic_clear_black_24dp:

<vector android:height="24dp" android:tint="#0D0B81"
    android:viewportHeight="24.0" android:viewportWidth="24.0"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

猜你喜欢

转载自blog.csdn.net/Mr_theSun/article/details/82729708
今日推荐