我有一个这样定义的 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/