如何以编程方式更改 AppCompat v21 工具栏主题?
How to change AppCompat v21 toolbar theme programmatically?
这是我的工具栏 xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
android:layout_height="@dimen/toolbar_height"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/primary_color">
</android.support.v7.widget.Toolbar>
我想以编程方式更改 app:theme。
我该怎么做?
您可以通过编程方式或使用样式来执行此操作:
Toolbar toolbar; // your toolbar
toolbar.setBackgroundColor(newColor); // i don't tested this method. Write if it's not working
toolbar.setTitleTextColor(titleColor); // if toolbar is white set title to black, if toolbar is black set title to white
或者你可以用风格来做:
添加attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="toolbarStyle" format="reference"/>
</resources>
现在更改 toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
android:layout_height="@dimen/toolbar_height"
app:theme="?attr/toolbarStyle"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/primary_color">
</android.support.v7.widget.Toolbar>
并且在 styles.xml 中(如果您没有创建它):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyStyle.Dark" parent="AppCompat.Theme">
<item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
<style name="MyStyle.Light" parent="AppCompat.Theme.Light">
<item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Light.ActionBar</item>
</style>
</resources>
如果您 select 第二种方法(带有样式),您必须重新启动 activity 并在 super.onCreate()
之前使用 setTheme 方法
希望对你有所帮助。
使用以下代码片段添加主题:
Toolbar toolbar;
toolbar.getContext().setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar);
这是我的工具栏 xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
android:layout_height="@dimen/toolbar_height"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/primary_color">
</android.support.v7.widget.Toolbar>
我想以编程方式更改 app:theme。 我该怎么做?
您可以通过编程方式或使用样式来执行此操作:
Toolbar toolbar; // your toolbar
toolbar.setBackgroundColor(newColor); // i don't tested this method. Write if it's not working
toolbar.setTitleTextColor(titleColor); // if toolbar is white set title to black, if toolbar is black set title to white
或者你可以用风格来做:
添加attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="toolbarStyle" format="reference"/>
</resources>
现在更改 toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
android:layout_height="@dimen/toolbar_height"
app:theme="?attr/toolbarStyle"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/primary_color">
</android.support.v7.widget.Toolbar>
并且在 styles.xml 中(如果您没有创建它):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyStyle.Dark" parent="AppCompat.Theme">
<item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
<style name="MyStyle.Light" parent="AppCompat.Theme.Light">
<item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Light.ActionBar</item>
</style>
</resources>
如果您 select 第二种方法(带有样式),您必须重新启动 activity 并在 super.onCreate()
之前使用 setTheme 方法希望对你有所帮助。
使用以下代码片段添加主题:
Toolbar toolbar;
toolbar.getContext().setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar);