Different formats for different Textview defined b

2019-08-13 03:50发布

I have a problem figuring out how to do this: I am currently coding an app that comes with different themes (User can select the complete appereance of the app out of a list of different styles). Then the list item is selected I want to call setTheme(R.style.Own_App_Style0); to change the complete appearance.

The problem is best explained by an example: Lets say we have 2 TextView.

Theme1 1. TextView: TextColor should be green and TextSize 15sp. 2. TextView: TextColor should be red and TextSize 10sp.

Theme2 1. TextView: TextColor should be blue and TextSize 10sp. 2. TextView: TextColor should be yellow and TextSize 10sp.

Of course I know that by setting <item name="textViewStyle">@android:style/Widget.TextView</item> I can change the default appearance of TextViews. But how can it be done to have lets say two (ore more) different types of TextView with different applied styles (and by xml)?

2条回答
在下西门庆
2楼-- · 2019-08-13 04:12

Found a solution (basically in this answer setTextAppearance through code referencing custom attribute). In case anyone else has this problem I shortly explain:

Declare in style.xml a attribute and in the actual style definition asign a value (reference) to this attribute:

<declare-styleable name="CustomTextView">
    <attr name="mainTextView" format="reference"/>            
</declare-styleable>

<style name="appstyle0" parent="android:style/Theme.Holo.Light">
    <item name="@attr/mainTextView">@style/CustomTextViewAppearance1</item>
    <item name="android:textViewStyle">@style/CustomTextViewAppearance2</item>
</style>

<style name="appstyle1" parent="android:style/Theme.Holo.Light">
    <item name="@attr/mainTextView">@style/CustomTextViewAppearance2</item>
    <item name="android:textViewStyle">@style/CustomTextViewAppearance1</item>
</style>
<style name="CustomTextViewAppearance1">
    <item name="android:textSize">10dip</item>
</style>
<style name="CustomTextViewAppearance2">
    <item name="android:textSize">30dip</item>
</style>

Now in the layout all textViews are like CustomTextViewAppearance2 (because this is set as standard in this style. And the textViews that should use the other style write into the definition:

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="blablabla"  
            style="?mainButtonTextView"/>

When you now call .setTheme (after restart the activity) the appearance of the textviews switch. Like this method you can define as many different types of View styles and switch between them only by calling .setTheme.

查看更多
孤傲高冷的网名
3楼-- · 2019-08-13 04:24

Unfortunately, styles are static once they are defined. To have an entire cascade of styles modified programmatically, you would have to change the definition of the style itself. Instead, all you can do is change the style that is assigned to a TextView (or whatever style-able object), as outlined in the question I linked to in my comment above.

查看更多
登录 后发表回答