如何在android 4.2中自定义android操作栏?

问题描述

经过大量搜索和测试,解决方案无法自定义操作栏。 This post确实不错,但没有帮助我。我想知道为什么在android中做这样简单的工作真的很难。

我在Android Studio,API 17和Android 4.2中使用“基本活动”创建了一个示例项目,希望自定义带有白色背景和深色文本颜色的操作栏,而无需在style.xml中更改“ colorPrimary”。

下面是我的代码,但是没有用,当然,我已经测试了其他方式,但是没有用,这是我最新的测试代码。是否可能因为API版本?

这在color.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#028a58</color>
    <color name="colorPrimaryDark">#005c3a</color>
    <color name="colorAccent">#03DAC5</color>
     //I added these lines
    <color name="colorWhite">#FFFFFF</color>
    <color name="colorDark">#4E4E4E</color>
</resources>

在style.xml中:

<resources>
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <!-- Customize your theme here. -->
      <item name="colorPrimary">@color/colorPrimary</item>
       <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name="colorAccent">@color/colorAccent</item>
      <item name="actionBarStyle">@style/MyActionBar</item>
 </style>
   <style name="AppTheme.NoActionBar">
      <item name="windowActionBar">false</item>
      <item name="windowNoTitle">true</item>
  </style>
  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
  <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

  <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.solid.Inverse">
     <item name="android:background"  >@color/colorWhite</item>
     <item name="background">@color/colorWhite</item>
     <item name="android:textColor">@color/colorDark</item>
   </style>
</resources>

已更新:

activity_main.xml:

<androidx.coordinatorlayout.widget.CoordinatorLayout
 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:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">

 <com.google.android.material.appbar.AppBarLayout
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:theme="@style/AppTheme.AppBarOverlay">  

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_main"/>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

解决方法

使用 conversation-driver ,您可以在应用主题中使用 conversation-drivers { conversation-driver { ... } conversation-driver { ... } conversation-driver { ... } } 属性:

ActionBar

actionBarStyle 属性:

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="actionBarStyle">@style/MyActionBar</item>
 </style>

  <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
       <item name="background">@color/...</item>
  </style>

使用 actionBarTheme ,您可以使用:

    布局中的
  • <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="actionBarTheme">@style/ThemeOverlay.ActionBar</item> </style> <style name="ThemeOverlay.ActionBar" parent=""> <item name="colorPrimary">@color/....</item> </style> 属性:
Toolbar
  • 使用 android:background 属性覆盖默认颜色:
    <androidx.appcompat.widget.Toolbar
        android:background="@color/...."
        ... />

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...