【问题标题】:Android theme error'sAndroid主题错误
【发布时间】:2015-11-24 11:29:25
【问题描述】:

我想为我的应用创建新主题(< 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>

这是截图: v21 及以上;

v21之前

以及来自资源的颜色:

【问题讨论】:

  • 这正是你从你的主题中得到的。你真正想要达到的目标是什么?
  • @johnrao07 颜色如i.stack.imgur.com/6cluO.jpg
  • @johnrao07 用于状态栏和工具栏

标签: android material-design android-theme


【解决方案1】:

代码已更正

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>"/>

【讨论】:

  • 谢谢,但我怎样才能将这个示例用于这两个主题?
  • 您有两个文件夹 >v21 和
  • 我在 Android 5 上关闭了 VM (
  • 完成。如果我评论这一行: setTheme(R.style.DriverNotesAppTheme);我也在 5 android 上关闭了 vm。 Log cat - 只是关闭虚拟机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多