android– 如何以编程方式更改AppCompat v21工具栏主题?

android– 如何以编程方式更改AppCompat v21工具栏主题?,第1张

概述这是我的工具栏xml<?xmlversion="1.0"encoding="utf-8"?><android.support.v7.widget.Toolbarxmlns:android="http://schemas.android.com/apkes/android"xmlns:app="http://schemas.android.com/apkes-auto"

这是我的工具栏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 toolbartoolbar.setBackgroundcolor(newcolor); // i don't tested this method. Write if it's not workingtoolbar.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>

如果选择第二种方法(带样式),则必须重新启动活动并在super.onCreate()之前使用settheme方法

我希望我帮助过你.

总结

以上是内存溢出为你收集整理的android – 如何以编程方式更改AppCompat v21工具栏主题?全部内容,希望文章能够帮你解决android – 如何以编程方式更改AppCompat v21工具栏主题?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1098412.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存