Spl*_*act 37 android background clickable android-linearlayout
我想将背景设置android.R.attr.selectableItemBackground为a LinearLayout.使用XML时没有问题(可行)
<LinearLayout
android:id="@+id/llMiner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:clickable="true" >
Run Code Online (Sandbox Code Playgroud)
...但我必须在java代码中这样做,所以我试过这个
llMiner.setClickable(true);
llMiner.setBackgroundResource(android.R.attr.selectableItemBackground);
Run Code Online (Sandbox Code Playgroud)
......它不起作用,事实上我得到了NotFoundException第二行.所以在我尝试了这个变体后认为资源是一个颜色.
llMiner.setClickable(true);
llMiner.setBackgroundColor(android.R.attr.selectableItemBackground);
Run Code Online (Sandbox Code Playgroud)
这个没有启动异常,但是......不起作用(按下时没有改变背景,但是状态改变按下它必须这样做)...任何建议?
Woo*_*Kim 98
你可以用这种方式.
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();
this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
textView.setBackgroundResource(outValue.resourceId);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19244 次 |
| 最近记录: |