如何设置背景亮点一的LinearLayout?如何设置、亮点、背景、LinearLayout

由网友(斑驳叻丶你我记忆)分享简介:我想背景 android.R.attr.selectableItemBackground 设置为的LinearLayout 。当使用XML不存在任何问题(它的工作原理)I want to set the background android.R.attr.selectableItemBackground to a L...

我想背景 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);
}
阅读全文

相关推荐

最新文章