【问题标题】:How do I change the Activity's theme using styles.xml at runtime如何在运行时使用 styles.xml 更改 Activity 的主题
【发布时间】:2018-07-28 04:46:01
【问题描述】:

styles.xml 中有 3 种不同类型的样式,如下所示:

<style name="Theme_A" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">#01AC50</item>
    <item name="colorPrimaryDark>#FF007838</item>
    <item name="colorAccent">#009688</item>"
<style>

用户可以通过按钮选择自己想要的样式,点击按钮后如何改变整个Activity的样式

【问题讨论】:

    标签: android android-activity android-xml android-theme


    【解决方案1】:

    How to setTheme to an activity at runtime? It doesn't work call setTheme before onCreate and setContentView有建议使用

    setContentView(...);
    setTheme(R.style.MyTheme);
    setContentView(...);
    

    即:看来你需要setContentViewsetTheme之后,as

    [setTheme] 应该在上下文中实例化任何视图之前调用

    【讨论】:

      【解决方案2】:

      试试这个:-

      你需要在setContentView()之前使用setTheme()函数

      正如文档所说,您需要在视图实例化之前定义主题。

      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
           setTheme(android.R.style.Theme_Dark);
           .
           .
           setContentView(R.layout.main);
      }
      

      【讨论】:

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