不同格式不同的TextView通过自定义样式定义 [英] Different formats for different Textview defined by Custom style

查看:139
本文介绍了不同格式不同的TextView通过自定义样式定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题搞清楚如何做到这一点:
我目前编码附带不同主题的应用程序(用户可以选择的应用程序的完整appereance出不同样式的列表中)。
然后,列表项中选择我想打电话给 setTheme(R.style.Own_App_Style0); 来改变外观完整

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.

问题最好是通过一个例子来解释:
比方说我们有2个TextView的。

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

THEME1
 1. TextView中:TEXTCOLOR应该是绿色和TEXTSIZE 15sp。
 2. TextView中:TEXTCOLOR应该是红色和TEXTSIZE 10SP

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

THEME2
 1. TextView中:TEXTCOLOR应该是蓝色和TEXTSIZE 10SP。
 2. TextView中:TEXTCOLOR应为黄色和TEXTSIZE 10SP。

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

我当然知道,通过设置<项目名称=textViewStyle> @android:风格/ Widget.TextView< /项目> 我可以更改默认TextViews外观。
但如何能不能做到有让说,两(或多个)不同类型的TextView的不同应用的样式(和XML)?

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)?

推荐答案

在发现这个答案<一个解决方案(基本上href=\"http://stackoverflow.com/questions/8757764/settextappearance-through-$c$c-referencing-custom-attribute\">setTextAppearance通过code引用自定义属性)。如果任何人有这个问题,我就会解释:

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

在声明一个style.xml属性,在实际的样式定义ASIGN值(参考)这个属性:

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>

现在布局的所有textViews像CustomTextViewAppearance2(因为这是这种风格设定为标准,应该使用其他样式写入的定义textViews:

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"/>

当你现在打电话.setTheme(后重新启动活动)的textviews的外观切换。像这样的方法,你只能通过调用.setTheme定义为许多不同类型的视图样式和它们之间的切换。

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.

这篇关于不同格式不同的TextView通过自定义样式定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆