如何设置android:background ="?android:attr/selectableItemBackground"?

Joh*_*nha 9 java xml android

我该如何以android:background="?android:attr/selectableItemBackground""编程方式完成?

我试过了mView.setBackgroundResource(android.R.attr.selectableItemBackground);,但没用.

kri*_*son 19

您需要先解析该属性.

    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);
    }
Run Code Online (Sandbox Code Playgroud)