繁体   English   中英

无法在我的应用程序的代码中设置主题

[英]Cannot set Theme in Code in my App

我无法在Android应用程序中实现主题选择。

之前曾问过这个问题,但是解决方案-在setContentView之前调用application.setTheme(theme_id)-对我不起作用。

styles.xml中定义了2个主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<!-- second theme, hard coded colors for testing -->
<style name="DarkTheme" parent="Theme.AppCompat.DayNight">
    <item name="colorPrimary">#ff0000</item>
    <item name="colorPrimaryDark">#00ff00</item>
    <item name="colorAccent">#0000ff</item>
</style>

这两个主题都可以在AndroidManifest.xml中使用:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    >
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

要么

<application
    ...
    android:theme="@style/DarkTheme"
    >
    ...
</application>

结果与预期的一样,样式不同。

但是主题选择应该由用户完成,因此我通过在清单中使用android:theme="@style/AppTheme"并对此进行了硬编码来测试了基本功能:

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // yes, the line appears in the log:
    Log.d("MYAPP", "calling application.setTheme(R.style.DarkTheme)")
    application.setTheme(R.style.DarkTheme)

    setContentView(R.layout.activity_main)
  }
}

结果:仍在使用AndroidManifest.xml中定义的主题,setTheme调用无效。

没有编译警告,没有运行时消息,没有可疑日志条目。

使用Kotlin,但这不应该是问题的原因。

最终,我设法使其工作。

要清楚:我首先发布的代码:

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    application.setTheme(R.style.DarkTheme)
    setContentView(R.layout.activity_main)
  }
}

根据关于SO的类似问题的许多答案,它应该起作用。

但事实并非如此。

还有这个:

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    application.setTheme(R.style.DarkTheme)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  }
}

或函数调用序列中的其他排列不能解决问题。

不是一个真正的解决方案,但是解决该问题的方法是BjörnKechel的答案,位于以下位置: 以编程方式更改活动的主题

覆盖Activity中的getTheme方法,而不是调用无功能的setTheme方法:

class MainActivity : AppCompatActivity() {

  protected var selectedTheme : String = ""
  lateinit protected var sharedPreferences : SharedPreferences
  var initialized = false

  fun init() {
    if (!initialized) {
      initialized = true
      sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
    }
    if ("" == selectedTheme) {
      selectedTheme = sharedPreferences.getString("selected_theme", "AppTheme")
    }
  }

  override fun getTheme(): Resources.Theme {
    init()
    val theme = super.getTheme()
    when(selectedTheme) {
      "DarkTheme" -> {
        theme.applyStyle(R.style.DarkTheme, true)
      }
      "AppTheme" -> {
        theme.applyStyle(R.style.AppTheme, true)
      }
    }
    return theme
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)

    settings.setOnClickListener {
      openSettings()
    }
  }

  fun openSettings() {
    val intent = Intent(baseContext, SettingsActivity::class.java)
    startActivity(intent)
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM