【问题标题】:applying themes to my android app dynamically将主题动态应用到我的 android 应用程序
【发布时间】:2012-02-10 04:34:02
【问题描述】:

我有三个按钮来更改主题。单击每个按钮时,我的应用程序主题必须动态更改。如何以编程方式进行。

【问题讨论】:

  • 抱歉,您不能以编程方式更改样式,请参阅here
  • 看看this

标签: android themes


【解决方案1】:

要在运行时动态设置主题,请在调用 setContentView() 之前在 Activity 的 onCreate() 方法中调用 setTheme()。要更改主题,您只需重新启动 Activity。

这是一个很好的tutorial,关于如何动态应用主题。

this 也是一个。

【讨论】:

    【解决方案2】:

    请访问此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">
    

    【讨论】:

    • 点击按钮时如何更改背景颜色
    【解决方案3】:

    抱歉,您不能以编程方式更改样式。

    how to set the Style Attribute Programmatically in Android?

    当然还有其他方法可以实现这种期望的行为。您可以为每个按钮设置 onclick 侦听器,并以编程方式更改各种视图元素的文本大小、颜色、背景等

    【讨论】:

      【解决方案4】:

      您可以为给定的 xml 文件使用特定的主题。 在图形布局中,您可以使用编辑配置更改布局的主题。

      使用 onclick 事件转到下一个布局,这里您的主题将与第一个不同。

      【讨论】:

        【解决方案5】:

        Vimalatha,要在单击按钮时更改背景,只需将此代码添加到按钮的 onClick 函数中即可。

         myLinearLayout.setBackgroundColor(Color.BLUE);
        

        假设 myLinearLayout 是您的 LinearLayout 名称...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多