maoruibin / maoruibin.github.com

personal blog
http://gudong.site
MIT License
21 stars 5 forks source link

通过代码方式设置 selectableItemBackground 背景 #66

Open maoruibin opened 6 years ago

maoruibin commented 6 years ago

xml 布局中设置按下背景

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:paddingRight="10dp">

代码设置

val outValue = TypedValue()
holder!!.itemView.context.theme.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true)
holder!!.itemView.setBackgroundResource(outValue.resourceId)