两种方法给任意View添加默认的点击效果

2017-04-09  本文已影响0人  今夜相思又几许
前提是此控件有点击事件

一、在布局文件中添加

在当前要添加的布局中添加以下一句话

android:background="?attr/selectableItemBackground"

二、用代码进行添加

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
int[] attribute = new int[]{android.R.attr.selectableItemBackground};
TypedArray typedArray = getTheme().obtainStyledAttributes(typedValue.resourceId, attribute);

然后再给需要添加的控件设置背景:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            textView.setBackground(typedArray.getDrawable(0));
        }

版本限制,所以需要添加判断语句


**
这样,在android5.0上面就会有涟漪的效果
**

上一篇 下一篇

猜你喜欢

热点阅读