开发者

How to get the default colors on Android

开发者 https://www.devze.com 2023-01-06 05:46 出处:网络
I have a custom component that I want to give the same colors as a TextView. That is, I don\'t want to copy its colors, I want to get the default background and foreground colors, if there\'s such a c

I have a custom component that I want to give the same colors as a TextView. That is, I don't want to copy its colors, I want to get the default background and foreground colors, if there's such a concept on android.

[Edit]

The followin开发者_StackOverflowg seems to yield the text color of my TextView. But is it just luck? It's not intuitive to me that a default TextView would use android.R.attr.textColorSecondary? And why does not resolveAttribute return the color directly?

TypedValue tv = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.textColorSecondary, tv, true);
Color holyColor = getResources().getColor(tv.resourceId);

[Edit]

I found the source code of TextView at android.git.kernel.org, but it seemed to contain a lot of referrences to com.android.internal.R, which I don't think I should use in my own code. I'm currently looking for some kind of evidence that TextView uses android.R.attr.textColorSecondary.

[Edit]

I found some kind of evidence at developer.android.com, in styles.xml that TextView uses android.R.attr.textAppearanceSmall. textAppearanceSmall is documented to default to "secondary text color".

I guess I was lucky after all, but I still don't like that little code snippet of mine.


What you're looking for are attributes. Attributes link widgets to styles. For example, android:background is what you'd set for a particular view, but there's attributes like android:panelBackground and android:windowBackground that you can use override to affect the system as a whole.

You should look through R.attr and then link to those attributes in your widget. There should be a number of them that are linked to TextView; it would make sense to download the Android source code and see which attributes are used.

0

精彩评论

暂无评论...
验证码 换一张
取 消