android - 以编程方式更改 AppBarLayout 主题?

标签 android styles themes android-appbarlayout

我有一个这样定义的 AppBarLayout

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

但是,有时我会更改应用程序的主题,我也想更改它及其子主题。通过这样做,我取得了部分成功:

    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
    appBarLayout.getContext().setTheme(R.style.AppTheme_Dark_AppBarOverlay);
    appBarLayout.invalidate();

但是,标题的 TextView 没有获取主题设置。

如何设置?

最佳答案

来自documentation for Context.setTheme() :

Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).

最重要的是,在 Context 上设置主题会影响稍后创建的 View 。这意味着您必须在 setTheme() 之后重建您的布局。

关于android - 以编程方式更改 AppBarLayout 主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163629/

相关文章:

java - 无法评估所选表达式? - Eclipse 库调试错误

c# - 在不同的 Android 设备上识别同一用户

Android 对单个 EditText 使用 colorControl

python - 如何复制word文档的内容?

具有可变皮肤/主题的 Android 应用程序

android - 从 android PayPal sdk 中提取运输信息

java - 如何检查元素是否具有特定标签? Jsoup安卓版

javascript - 将元素放置在 react native 的右上角

html - 为 tumblr 主题上的每个标签设置不同的样式

css - JavaFX Gluon 主题问题