Den*_*nis 106 android themes toolbar
我正在使用新的v7工具栏,但在我的生活中无法弄清楚如何更改标题的颜色.我已将工具栏的@style设置为styles.xml中声明的样式,并将textTextStyle应用于textColor.我错过了什么吗?我正在为Lollipop编码,但目前在Kitkat设备上进行测试.
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="ActionBar" parent="Theme.AppCompat">
<item name="android:background">@color/actionbar_background</item>
<item name="android:titleTextStyle">@style/ActionBar.TitleTextStyle</item>
</style>
<style name="ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/actionbar_title_text</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
actionbar.xml:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
style="@style/ActionBar"/>
Run Code Online (Sandbox Code Playgroud)
Eug*_*nec 222
从appcompat-v7-r23开始,您可以直接在您Toolbar的样式上使用以下属性:
app:titleTextColor="@color/primary_text"
app:subtitleTextColor="@color/secondary_text"
Run Code Online (Sandbox Code Playgroud)
如果您的最小SDK为23,并且您使用native,则Toolbar只需将名称空间前缀更改为android.
在Java中,您可以使用以下方法:
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setSubtitleTextColor(Color.WHITE);
Run Code Online (Sandbox Code Playgroud)
这些方法采用颜色int而不是颜色资源ID!
<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"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.MyApp.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
style="@style/Widget.MyApp.Toolbar.Solid"/>
Run Code Online (Sandbox Code Playgroud)
<style name="Widget.MyApp.Toolbar.Solid" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/actionbar_color</item>
<item name="android:elevation" tools:ignore="NewApi">4dp</item>
<item name="titleTextAppearance">...</item>
</style>
<style name="ThemeOverlay.MyApp.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
<!-- Parent theme sets colorControlNormal to textColorPrimary. -->
<item name="android:textColorPrimary">@color/actionbar_title_text</item>
</style>
Run Code Online (Sandbox Code Playgroud)
@PeterKnut报告这会影响溢出按钮,导航抽屉按钮和后退按钮的颜色.它也改变了文本的颜色SearchView.
关于图标颜色:colorControlNormal继承自
android:textColorPrimary 黑暗主题(黑底白字)android:textColorSecondary 用于浅色主题(黑色白色)如果将此应用于操作栏的主题,则可以自定义图标颜色.
<item name="colorControlNormal">#de000000</item>
Run Code Online (Sandbox Code Playgroud)
appcompat-v7到r23有一个错误,它要求你也像这样覆盖原生对应物:
<item name="android:colorControlNormal" tools:ignore="NewApi">?colorControlNormal</item>
Run Code Online (Sandbox Code Playgroud)
注意:此部分可能已过时.
由于您使用搜索小部件由于某种原因使用不同的后箭头(技术上不是视觉上,技术上)而不是appcompat-v7附带的后箭头,您必须在应用程序的主题中手动设置它.支持库的drawable正确着色.否则它总是白色的.
<item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
Run Code Online (Sandbox Code Playgroud)
至于搜索视图文本......没有简单的方法.在深入挖掘其源代码后,我找到了一种获取文本视图的方法.我没有测试过这个,所以如果这不起作用,请在评论中告诉我.
SearchView sv = ...; // get your search view instance in onCreateOptionsMenu
// prefix identifier with "android:" if you're using native SearchView
TextView tv = sv.findViewById(getResources().getIdentifier("id/search_src_text", null, null));
tv.setTextColor(Color.GREEN); // and of course specify your own color
Run Code Online (Sandbox Code Playgroud)
适用于默认操作appcompat-v7操作栏的样式如下所示:
<!-- ActionBar vs Toolbar. -->
<style name="Widget.MyApp.ActionBar.Solid" parent="Widget.AppCompat.ActionBar.Solid">
<item name="background">@color/actionbar_color</item> <!-- No prefix. -->
<item name="elevation">4dp</item> <!-- No prefix. -->
<item name="titleTextStyle">...</item> <!-- Style vs appearance. -->
</style>
<style name="Theme.MyApp" parent="Theme.AppCompat">
<item name="actionBarStyle">@style/Widget.MyApp.ActionBar.Solid</item>
<item name="actionBarTheme">@style/ThemeOverlay.MyApp.ActionBar</item>
<item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Pet*_*nut 37
如果您只需要更改标题的颜色而不是搜索小部件中的文本颜色,这是我的解决方案.
布局/ toolbar.xml
<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:background="@color/toolbar_bg"
app:theme="@style/AppTheme.Toolbar"
app:titleTextAppearance="@style/AppTheme.Toolbar.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"/>
Run Code Online (Sandbox Code Playgroud)
值/的themes.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
</style>
<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.ActionBar">
<!-- Customize color of navigation drawer icon and back arrow -->
<item name="colorControlNormal">@color/toolbar_icon</item>
</style>
<style name="AppTheme.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<!-- Set proper title size -->
<item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item>
<!-- Set title color -->
<item name="android:textColor">@color/toolbar_title</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
以类似的方式,您还可以设置subtitleTextAppearance.
fra*_*nch 11
如果您支持API 23及更高版本,则现在可以使用titleTextColor属性来设置工具栏的标题颜色.
布局/ toolbar.xml
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:titleTextColor="@color/colorPrimary"
/>
Run Code Online (Sandbox Code Playgroud)
MyActivity.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar)
toolbar.setTitleTextColor(Color.WHITE);
Run Code Online (Sandbox Code Playgroud)
hun*_*dym 10
在Android 4.4和6.0上为我设置app:titleTextColor我的android.support.v7.widget.Toolbar作品com.android.support:appcompat-v7:23.1.0:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="@android:color/white" />
Run Code Online (Sandbox Code Playgroud)
这让我烦恼了一段时间,我不喜欢给出的任何答案,所以我看了一下源码,看看它是如何工作的.
FractalWrench位于正确的路径上,但它可以在API 23下使用,而不必在它自己的工具栏上设置.
正如其他人所说,您可以在工具栏上设置样式
app:theme="@style/ActionBar"
Run Code Online (Sandbox Code Playgroud)
在该样式中,您可以设置标题文本颜色
<item name="titleTextColor">#00f</item>
Run Code Online (Sandbox Code Playgroud)
适用于API 23或23+
<item name="android:titleTextColor">your colour</item>
Run Code Online (Sandbox Code Playgroud)
完整的xml
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:theme="@style/ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>
<style name="ActionBar" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<item name="android:titleTextStyle">@style/ActionBarTextStyle</item>
<item name="titleTextColor">your colour</item>
<item name="android:background">#ff9900</item>
</style>
Run Code Online (Sandbox Code Playgroud)
下面是可以为工具栏设置的所有属性
<declare-styleable name="Toolbar">
<attr name="titleTextAppearance" format="reference" />
<attr name="subtitleTextAppearance" format="reference" />
<attr name="title" />
<attr name="subtitle" />
<attr name="gravity" />
<attr name="titleMargins" format="dimension" />
<attr name="titleMarginStart" format="dimension" />
<attr name="titleMarginEnd" format="dimension" />
<attr name="titleMarginTop" format="dimension" />
<attr name="titleMarginBottom" format="dimension" />
<attr name="contentInsetStart" />
<attr name="contentInsetEnd" />
<attr name="contentInsetLeft" />
<attr name="contentInsetRight" />
<attr name="maxButtonHeight" format="dimension" />
<attr name="navigationButtonStyle" format="reference" />
<attr name="buttonGravity">
<!-- Push object to the top of its container, not changing its size. -->
<flag name="top" value="0x30" />
<!-- Push object to the bottom of its container, not changing its size. -->
<flag name="bottom" value="0x50" />
</attr>
<!-- Icon drawable to use for the collapse button. -->
<attr name="collapseIcon" format="reference" />
<!-- Text to set as the content description for the collapse button. -->
<attr name="collapseContentDescription" format="string" />
<!-- Reference to a theme that should be used to inflate popups
shown by widgets in the toolbar. -->
<attr name="popupTheme" format="reference" />
<!-- Icon drawable to use for the navigation button located at
the start of the toolbar. -->
<attr name="navigationIcon" format="reference" />
<!-- Text to set as the content description for the navigation button
located at the start of the toolbar. -->
<attr name="navigationContentDescription" format="string" />
<!-- Drawable to set as the logo that appears at the starting side of
the Toolbar, just after the navigation button. -->
<attr name="logo" />
<!-- A content description string to describe the appearance of the
associated logo image. -->
<attr name="logoDescription" format="string" />
<!-- A color to apply to the title string. -->
<attr name="titleTextColor" format="color" />
<!-- A color to apply to the subtitle string. -->
<attr name="subtitleTextColor" format="color" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
通过材质组件库,您可以使用该app:titleTextColor属性。
在布局中你可以使用类似的东西:
<com.google.android.material.appbar.MaterialToolbar
app:titleTextColor="@color/...."
.../>
Run Code Online (Sandbox Code Playgroud)
您还可以使用自定义样式:
<com.google.android.material.appbar.MaterialToolbar
style="@style/MyToolbarStyle"
.../>
Run Code Online (Sandbox Code Playgroud)
with(扩展Widget.MaterialComponents.Toolbar.Primary样式):
<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar.Primary">
<item name="titleTextColor">@color/....</item>
</style>
Run Code Online (Sandbox Code Playgroud)
或(扩展Widget.MaterialComponents.Toolbar样式):
<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="titleTextColor">@color/....</item>
</style>
Run Code Online (Sandbox Code Playgroud)
您还可以使用属性覆盖样式定义的颜色android:theme(使用Widget.MaterialComponents.Toolbar.Primary样式):
<com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:theme="@style/MyThemeOverlay_Toolbar"
/>
Run Code Online (Sandbox Code Playgroud)
和:
<style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- This attributes is also used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/...</item>
</style>
Run Code Online (Sandbox Code Playgroud)
或(使用Widget.MaterialComponents.Toolbar样式):
<com.google.android.material.appbar.MaterialToolbar
style="@style/Widget.MaterialComponents.Toolbar"
android:theme="@style/MyThemeOverlay_Toolbar2"
Run Code Online (Sandbox Code Playgroud)
和:
<style name="MyThemeOverlay_Toolbar3" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- This attributes is used by title -->
<item name="android:textColorPrimary">@color/white</item>
<!-- This attributes is used by navigation icon and overflow icon -->
<item name="colorOnPrimary">@color/secondaryColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
137229 次 |
| 最近记录: |