【问题标题】:How to change actionbar color in code with AppCompat style?如何使用 AppCompat 样式更改代码中的操作栏颜色?
【发布时间】:2019-08-09 09:50:04
【问题描述】:

这是我的 main_activity.xml,下面你可以看到我正在使用的样式

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/nav_host_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemIconTint="@color/color_white"
            app:itemTextColor="@color/color_white"
            app:menu="@menu/bottom_nav" />
    </LinearLayout>
</layout>




  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:colorPrimary">#3f51b5</item>
        <item name="android:textColorPrimary">#FF0000</item>
        <item name="colorPrimary">#3f51b5</item>
  </style>

并且以编程方式我不知道如何更改操作栏标题颜色,实际上我也无法更改 xml 中的文本颜色(但后一个我只是想知道好奇)我需要以编程方式更改操作栏标题颜色

【问题讨论】:

    标签: android android-actionbar android-appcompat


    【解决方案1】:

    尝试使用此代码,希望对您有所帮助!

      getActionBar().setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));
    

    【讨论】:

    • 我想更改文本颜色而不是操作栏的背景颜色
    • 我把它放在 onCreate 它崩溃说 nullpointerexception 而我的主题是 Theme.AppCompat.Light.DarkActionBar
    • 我已更改为 supportActionBar 但它不会改变颜色和文本
    • 实际上它可以工作,问题是我有底部导航,在导航图中我有标签,这是压倒一切的,但是当我将你的代码移动到每个片段时它可以工作,所以谢谢
    猜你喜欢
    • 2014-06-03
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多