【发布时间】:2015-01-28 09:19:24
【问题描述】:
我正在使用最新的 SDK 版本 (API 21) 和支持库 21.0.2 进行开发,但在尝试实施新的 Material Design 指南时遇到了麻烦。
Material Design 说我需要我的primary color 和我的accent color 并将它们应用到我的应用程序上。但有时当我打开应用程序时,primary color 在某些小部件中变得透明,它会恢复正常,直到我关闭应用程序(使用后退按钮)并再次启动它。
这是primary color 在我的工具栏中透明的示例。
我使用 Teal 500 作为我的原色,如您所见,它仅在 android.support.v7.widget.Toolbar 中是透明的。这也发生在我的Navigation Drawer 和(有时,有时不是)另一个随机小部件中。
这是我的工具栏
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" >
我已尝试使用@color/primary 和?attr/colorPrimary,但没有成功。
这是我的Theme(我不知道是否相关,但以防万一):
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">#fff</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:textViewStyle">@style/Widget.TextView.White</item>
</style>
primary color 会发生这种情况,accent color 工作正常。我的设备运行的是 4.2.2,我还没有检查更多设备。
工具栏活动
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" />
</LinearLayout>
<!-- The navigation drawer -->
<LinearLayout
android:id="@+id/navdrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<!-- IN THIS IT ALSO HAPPENS -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:orientation="vertical"
android:background="@color/primary" >
<!-- Some stuff -->
</LinearLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/black_light"
android:divider="@color/grey"
android:choiceMode="singleChoice" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
-
能否将您的活动的 xml 添加到您添加此工具栏的位置?
-
@NguyễnHoàiNam 完成,请查看我的编辑。提前致谢,我认为这与活动本身无关,因为它也发生在其他活动中。
-
好吧,既然你的主题是正确的(NoActionBar 和 colorPrimary 属性等),我想确保你没有定义你的 values-v11 样式和 values-v14 样式。它可能会覆盖您最近在 4.2.2 设备上的主题。并假设您没有对工具栏进行任何编程修改(在代码中),我将尝试在我的设备上测试您的代码。
-
@NguyễnHoàiNam 谢谢,不,这个主题在
values和values-21是空的,目前我没有更多的values-vX文件夹。我正在关注official Navigation Drawer guide,并且我没有以其他方法触摸工具栏。只是说工具栏是一个示例,但这发生在更多随机小部件(即 TextView)中。 -
这很奇怪(好吧,正如预期的那样)。考虑参考这个问题? stackoverflow.com/questions/2118251/… 。如果没有帮助,我想知道您的清单(您在哪里使用主题,在 Application 标签或 Activity 标签中),出现奇怪行为的活动类,以及您的 Widget.TextView.White 定义。但首先要考虑使用上下文的正确方法。
标签: android android-theme android-5.0-lollipop android-appcompat material-design