【发布时间】:2012-02-10 04:34:02
【问题描述】:
我有三个按钮来更改主题。单击每个按钮时,我的应用程序主题必须动态更改。如何以编程方式进行。
【问题讨论】:
我有三个按钮来更改主题。单击每个按钮时,我的应用程序主题必须动态更改。如何以编程方式进行。
【问题讨论】:
请访问此link。
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="@string/hello" />
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
通过定义一个xml主题文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
您还可以将样式应用于应用程序的所有活动:
<application android:theme="@style/CustomTheme">
或者只是一项活动:
<activity android:theme="@android:style/Theme.Dialog">
【讨论】:
抱歉,您不能以编程方式更改样式。
how to set the Style Attribute Programmatically in Android?
当然还有其他方法可以实现这种期望的行为。您可以为每个按钮设置 onclick 侦听器,并以编程方式更改各种视图元素的文本大小、颜色、背景等
【讨论】:
您可以为给定的 xml 文件使用特定的主题。 在图形布局中,您可以使用编辑配置更改布局的主题。
使用 onclick 事件转到下一个布局,这里您的主题将与第一个不同。
【讨论】:
Vimalatha,要在单击按钮时更改背景,只需将此代码添加到按钮的 onClick 函数中即可。
myLinearLayout.setBackgroundColor(Color.BLUE);
假设 myLinearLayout 是您的 LinearLayout 名称...
【讨论】: