Android 主题错误的

标签 android material-design android-theme

我想为我的应用制作新主题(一个主题在 < v21 之前,一个主题在 >=v21 之后)。

这是来自 Activity 的代码:

  protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.DriverNotesAppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_bar_test);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
}

XML 布局工具栏:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    app:popupTheme="@style/DriverNotesAppTheme"
    app:theme="@style/DriverNotesAppTheme">

这是v21之前的主题:

<style name="DriverNotesAppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_bg</item>
    <item name="colorPrimaryDark">@color/status_bar</item>
    <item name="colorAccent">@color/edittext_primary</item>
    <item name="android:windowBackground">@color/light_blue</item>
</style>

这是 v21 的主题及更高:

<style name="DriverNotesAppTheme" parent="android:Theme.Material">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_bg</item>
    <item name="colorPrimaryDark">@color/status_bar</item>
    <item name="colorAccent">@color/edittext_primary</item>
    <item name="android:windowBackground">@color/light_blue</item>
</style>

这是截图: enter image description here v21及更高;

enter image description here

之前v21

以及来自资源的颜色: enter image description here

最佳答案

代码更正

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tool_bar_test);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

在您的工具栏中设置主题

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
app:popupTheme="@style/<Your Theme Name>"
app:theme="@style/<Your Theme Name>"/>

关于Android 主题错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892464/

相关文章:

android - SIGSEGV SEGV_MAPERR 0x00000000fbadbeef 在 libwebviewchromium.so

android - 错误 : Program type already present: android. support.v4.accessibilityservice.AccessibilityServiceInfoCompat

css - 从 md-list-item(AngularJS Material )中删除点击行为

android - 更改为新的 materin 主题后缺少后退按钮

android - 渐变在 Android M 上因 IllegalArgumentException 而崩溃

android - 如何在 Android 中保留 4.1 主题并删除 TitleBar?

android - 所选小部件的主题相关颜色

java - 无法从 Linkify 模式链接到新 Activity

java - android:根据FCM通知的数据采取行动

android - 膨胀类 RecyclerView 时出错