gpt4 book ai didi

java - 如何以编程方式设置android :background ="?android:attr/selectableItemBackground"?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:01 26 4
gpt4 key购买 nike

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

我尝试了 mView.setBackgroundResource(android.R.attr.selectableItemBackground); 但它没有用。

最佳答案

您需要先解析属性。

    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);
}

关于java - 如何以编程方式设置android :background ="?android:attr/selectableItemBackground"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38527508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com