Phi*_*ord 6 android android-theme android-styles
关于 Android 主题和样式的问题和文章数以百万计——我已经阅读了其中的一半,但我仍然会发疯。
我想要的 - 并且来自具有可爱,友好的旧 CSS 样式表的 Web 应用程序开发背景,我认为这很容易 - 就是为我的应用程序创建一个主题,并在该主题中指定我的工具栏的颜色(我是为每个活动指定它们,我认为这是阅读官方文档后正确的做法)。
所以,在我的styles.xml文件中,我有:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:toolbarStyle">@style/MyToolbar</item>
</style>
<style name="MyToolbar" parent="Base.Widget.AppCompat.Toolbar">
<item name="android:textColor">#ffffff</item>
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:elevation">4dp</item>
<item name="android:background">@color/colorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在我看来,我在这里为我的工具栏创建了一个样式,然后在我的主题中引用了该样式。然后我将我的主题设置为应用程序主题AndroidManifest.xml:
android:theme="@style/MyTheme"
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,这是有效的。我的主题中引用了其他样式,为了简单起见,我从代码中省略了这些样式,它们可以正常工作 - 我的按钮、文本视图等按照我创建的样式设置样式。
但讨厌的工具栏不是。我试过直接在工具栏 xml 中引用我的样式(即使我不明白为什么我需要在主题中指定它):
<android.support.v7.widget.Toolbar
android:id="@+id/main_activity_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/MyToolbar">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
令人沮丧的是,这确实导致工具栏的背景颜色与我想要的一样,但文本仍然是黑色的,即使我试图将其设置为白色。
我是否误解了主题引用样式然后应用于活动或整个应用程序的方式?工具栏有什么独特之处吗?我花了更长的时间来尝试解决这个问题,而不是从头开始编写整个应用程序的其余部分,这是我的第一个 Android 应用程序!
更新: 好的,所以我已经确定,虽然我的工具栏的背景颜色需要在应用于工具栏的样式中设置,以防止该背景颜色传播到所有子视图,但需要在主题中指定所需的文本颜色因为工具栏中显示的文本是子视图(即使我在活动的 xml 中看不到它)。所以,这有效:
样式.xml:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:buttonStyle">@style/MyButton</item>
<item name="android:editTextStyle">@style/MyEditText</item>
</style>
<style name="MyToolbarStyle" parent="Base.Widget.AppCompat.Toolbar">
<item name="android:elevation">4dp</item>
<item name="android:background">@color/colorPrimary</item>
</style>
<style name="MyToolbarTheme" parent="Base.Widget.AppCompat.Toolbar">
<item name="android:textColorPrimary">#ffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)
main_activity.xml:
<android.support.v7.widget.Toolbar
android:id="@+id/main_activity_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/MyToolbarStyle"
app:theme="@style/MyToolbarTheme">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
所以我想我已经实现了我想要的,但我还没有回答我最初的问题:为什么我的主题没有隐式应用工具栏样式?为什么我必须在工具栏的标记中明确声明它?
做这样的事情:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_activity_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/MyToolbar"
app:popupTheme="@style/TextStyle"/>
Run Code Online (Sandbox Code Playgroud)
此外,请阅读此处的信息以了解有关风格和主题的想法。请参阅此处的链接。
| 归档时间: |
|
| 查看次数: |
4505 次 |
| 最近记录: |