如何设置背景亮点一的LinearLayout? [英] How to set background highlight to a LinearLayout?

查看:338
本文介绍了如何设置背景亮点一的LinearLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想背景 android.R.attr.selectableItemBackground 设置为的LinearLayout 。当使用XML不存在任何问题(它的工作原理)

I want to set the background android.R.attr.selectableItemBackground to a LinearLayout. When using XML there are no problems (it works)

<LinearLayout
    android:id="@+id/llMiner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true" >

...但我不得不这样做在java中code,所以我尝试这样做

... but I have to do this in java code, so I've tried this

llMiner.setClickable(true);
llMiner.setBackgroundResource(android.R.attr.selectableItemBackground);

...而这是行不通的,事实上我获得了 NotFoundException 在第二行。 所以,我已经试过后,这个变种认为该资源是颜色。

... and it doesn't work, infact I get a NotFoundException on this second line. So after I've tried this variant thinking that the resource is a Color.

llMiner.setClickable(true);
llMiner.setBackgroundColor(android.R.attr.selectableItemBackground);

这人不启动异常,但......不能正常工作(没有变化的背景下,当pressing,但在pssed,因为它需要做的$ P $的状态变化)...任何建议?

This one doesn't launch exception, but... doesn't work (there are no changing of background when pressing, but the state change in pressed as it have to do)... any suggestion?

推荐答案

您可以使用这种方式。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // If we're running on Honeycomb or newer, then we can use the Theme's
    // selectableItemBackground to ensure that the View has a pressed state
    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    textView.setBackgroundResource(outValue.resourceId);
}

这篇关于如何设置背景亮点一的LinearLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆