2016-07-22 75 views
3

我該怎麼辦android:background="?android:attr/selectableItemBackground""編程?機器人:ATTR /編程

我試圖mView.setBackgroundResource(android.R.attr.selectableItemBackground);並沒有奏效。

回答

8

您需要首先解決的屬性。

TypedValue typedValue = new TypedValue(); 

    // I used getActivity() as if you were calling from a fragment. 
    // You just want to call getTheme() on the current activity, however you can get it 
    getActivity().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true); 

    // it's probably a good idea to check if the color wasn't specified as a resource 
    if (typedValue.resourceId != 0) { 
     mView.setBackgroundResource(typedValue.resourceId); 
    } else { 
     // this should work whether there was a resource id or not 
     mView.setBackgroundColor(typedValue.data); 
    } 
+0

按預期工作,非常感謝! –