문제

My app has text that can be either black or white; small, medium, or large; and bold or not bold. I can't figure out a way to make styles for covering all bases without resorting to just including a ton of styles and repeating everything. Is there a way to reduce this somehow or apply more than one at a time?

<style name="Text">
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">#000000</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="Text.Small">
    <item name="android:textSize">8sp</item>
</style>

<style name="Text.Large">
    <item name="android:textSize">18sp</item>
</style>

<style name="Text.Small.Bold">
    <item name="android:textStyle">bold</item>
</style>

<style name="Text.Large.Bold">
    <item name="android:textStyle">bold</item>
</style>

<style name="Text.White">
    <item name="android:textColor">#ffffff</item>
</style>

<style name="Text.White.Small">
    <item name="android:textSize">8sp</item>
</style>

<style name="Text.White.Large">
    <item name="android:textSize">18sp</item>
</style>

<style name="Text.White.Small.Bold">
    <item name="android:textStyle">bold</item>
</style>

<style name="Text.White.Large.Bold">
    <item name="android:textStyle">bold</item>
</style>

Half of these are literally duplicates that inherit from a different parent style. I feel like this would be pretty trivial to accomplish in CSS without repeating everything like that. Am I stuck doing it this way in Android? Or is there something I am overlooking here?

도움이 되었습니까?

해결책 2

I have dug but there doesn't seem to be a way to do this native to Android. I am just going to leave it with the duplicated parts in each style and suffice it to say that this is the DRYest it can be in this context.

다른 팁

To avoid code duplication you could declare the item only once, and refer it from the others styles using the parent attribute.

Example using the code you provided :

<style name="Text.Small.Bold">
    <item name="android:textStyle">bold</item>
</style>

<style name="Text.Large.Bold">
    <item name="android:textStyle">bold</item>
</style>

could be changed in :

<style name="Text.Small.Bold">
    <item name="android:textStyle">bold</item>
</style>

<style name="Text.Large.Bold" parent="Text.Small.Bold" />
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top