2012-01-06 41 views
1

我有一个问题想清楚如何做到这一点: 我目前正在编写一个带有不同主题的应用程序(用户可以从列表中选择应用程序的完整上下文不同的风格)。 然后选择列表项我想调用setTheme(R.style.Own_App_Style0);来更改完整的外观。自定义样式定义的不同格式适用于不同的Textview

的问题是最好的例子来说明: 比方说我们有2个TextView的。

THEME1 1的TextView:TEXTCOLOR应该是绿色和TEXTSIZE 15sp。 2. TextView:TextColor应该是红色和TextSize 10sp。

THEME2 1的TextView:TEXTCOLOR应该是蓝色和TEXTSIZE 10SP。 2. TextView:TextColor应该是黄色和TextSize 10sp。

我当然知道,通过设置<item name="textViewStyle">@android:style/Widget.TextView</item>我可以改变TextViews的默认外观。 但是,如何让具有不同应用样式(和xml)的两种(更多)不同类型的TextView实现呢?

+0

看到这个问题的答案: http://stackoverflow.com/questions/4630440/how-to-change-a-textviews-style-at-runtime – elijah 2012-01-06 17:19:36

+0

不是很excctly。我想稍后在程序中只调用setTheme(R.style.OwnAppStyle);改变完整的布局。在发生问题时不会更改单独的小部件。我在这个问题上加了这个。 – KarlKarlsom 2012-01-06 17:31:20

回答

1

实测值的溶液(基本上在此答案setTextAppearance through code referencing custom attribute)。如果任何人有这个问题,我就会解释:

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

<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:

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

现在,当您拨打.setTheme(重新启动活动之后)的外观的文字浏览开关。像这种方法一样,您可以定义许多不同类型的视图样式,只需调用.setTheme即可在它们之间切换。

+0

谢谢,这有助于我!我期待有一个像“选择器”的东西: 10dip – dalf 2014-03-27 13:09:30

0

不幸的是,一旦被定义的样式是静态的。要以编程方式修改完整的样式级联,您必须更改样式本身的定义。相反,你所能做的就是改变分配给TextView(或者任何可用风格的对象)的风格,正如我在上面评论中链接到的问题中所述。

相关问题