banner



How To Change Action Bar Title Color In Android

Bagaimana cara mengubah warna latar belakang ActionBar dari ActionBarActivity menggunakan XML?


Detail:

Saya memperluas ActionBarActivity.
Eclipse dan SDK sepenuhnya ditambal pada 2011-11-06.

                                  <uses-sdk                                                      android:minSdkVersion                  =                  "4"                                                      android:targetSdkVersion                  =                  "14"                                                      />                                                                  

Dikerahkan ke perangkat Samsung dengan
Aplikasi Android 2.3.3android:theme="@android:style/Theme.Light"

Masalah: aplikasi ringan, tetapi ActionBar berwarna biru dengan ikon abu-abu, hampir tidak terlihat terhadap warna latar belakang biru. Saya juga ingin ActionBar menjadi ringan, sehingga ikon abu-abu lebih terlihat.

Saya sudah mencoba memodifikasi gaya tetapi tidak berhasil.
Saya mungkin melewatkan sesuatu yang sepele.

Bagaimana cara mengubah warna latar belakang ActionBar dari ActionBarActivity menggunakan XML?




Jawaban:


Sesuai dokumentasi - "Anda dapat mengontrol perilaku dan visibilitas bilah tindakan dengan API ActionBar, yang ditambahkan di Android 3.0 (API level 11)."

Jadi, ActionBar tidak akan berfungsi untuk lingkungan target Anda yang berada di level API 10 (Android 2.3.3).

Untuk jaga-jaga, jika Anda menargetkan API level 11 minimum, Anda dapat mengubah warna latar belakang ActionBar dengan mendefinisikan gaya khusus, seperti:

                                  <resources>                                                      <style                                                      name                  =                  "MyTheme"                                                      parent                  =                  "@android:style/Theme.Holo.Light"                  >                                                      <                  item name                  =                  "android:actionBarStyle"                  >@                  style                  /                  MyActionBar                  </                  item                  >                                                      </style>                                                      <style                                                      name                  =                  "MyActionBar"                                                      parent                  =                  "@android:style/Widget.Holo.Light.ActionBar"                  >                                                      <                  item name                  =                  "android:background"                  >                  ANY_HEX_COLOR_CODE                  </                  item                  >                                                      </style>                                                      </resources>                              

Dan, atur "MyTheme" sebagai tema untuk aplikasi / aktivitas.







Coba ini

                                  ActionBar                                      bar                                    =                                      getActionBar                  ();                                      bar                  .                  setBackgroundDrawable                  (                  new                                                      ColorDrawable                  (                  "COLOR"                  ));                              






coba ini:

                                  ActionBar                                      bar                                    =                                      getActionBar                  ();                                      bar                  .                  setBackgroundDrawable                  (                  new                                                      ColorDrawable                  (                  Color                  .                  parseColor                  (                  "#0000ff"                  )));                              





Gunakan ini - http://jgilfelt.github.io/android-actionbarstylegenerator/

Ini alat luar biasa yang memungkinkan Anda menyesuaikan bilah tindakan dengan pratinjau langsung.

Saya mencoba jawaban sebelumnya, tetapi selalu memiliki masalah dengan mengubah warna lain seperti tab dan huruf dan menghabiskan waktu berjam-jam untuk memperbaiki hal-hal. Alat ini membantu saya menyelesaikan desain hanya dalam beberapa menit.

Berikut screenshot alat ini

Semua yang terbaik! :)





Saya memiliki masalah yang sama, di mana Bilah Tindakan saya akan berubah menjadi abu-abu ketika saya memasukkan kode itu. Kemungkinan style sheet asli Anda terlihat seperti ini:

                              <style                                                name                =                "AppBaseTheme"                                                parent                =                "android:Theme.Holo.Light.DarkActionBar"                >                                                <!--                                  API                                14                                  theme customizations can go here                .                                                -->                                                </style>                          

"DarkActionBar" adalah yang membuat Action Bar Anda berwarna abu-abu. Saya mengubahnya menjadi ini, dan berhasil:

                              <style                                                name                =                "AppBaseTheme"                                                parent                =                "android:Theme.Holo.Light"                >                                                <!--                                  API                                14                                  theme customizations can go here                .                                                -->                                                <                item name                =                "android:actionBarStyle"                >@                style                /                MyActionBar                </                item                >                                                </style>                                                <style                                                name                =                "MyActionBar"                                                parent                =                "@android:style/Widget.Holo.Light.ActionBar"                >                                                <                item name                =                "android:background"                >#                2aa4cd                </                item                >                                                <                item name                =                "android:titleTextStyle"                >@                style                /                Theme                .                MyAppTheme                .                ActionBar                .                TitleTextStyle                </                item                >                                                </style>                                                <style                                                name                =                "Theme.MyAppTheme.ActionBar.TitleTextStyle"                                                parent                =                "android:style/TextAppearance.Holo.Widget.ActionBar.Title"                >                                                <                item name                =                "android:textColor"                >#                FFFFFF                </                item                >                                                </style>                                                          

Saya juga melemparkan cara mengedit warna teks. Juga, tidak perlu mengubah apa pun di sekitar sumber daya.

-Warren


Perilaku Actionbar juga dapat diubah dalam API <11

Lihat Dokumentasi Resmi Android untuk referensi

Saya membuat aplikasi minSdkVersion = "9"dan targetSdkVersion = "21"mengubah warna bilah tindakan dan berfungsi baik dengan API level 9

Ini adalah xml

res/values/themes.xml

                                  <?                  xml version                  =                  "1.0"                                      encoding                  =                  "utf-8"                  ?>                                                      <resources>                                                      <!-- the theme applied to the application or activity -->                                                      <style                                                      name                  =                  "CustomActionBarTheme"                                                      parent                  =                  "@style/Theme.AppCompat.Light.DarkActionBar"                  >                                                      <                  item name                  =                  "android:actionBarStyle"                  >@                  style                  /                  MyActionBar                  </                  item                  >                                                      <!--                                      Support library compatibility                                    -->                                                      <                  item name                  =                  "actionBarStyle"                  >@                  style                  /                  MyActionBar                  </                  item                  >                                                      </style>                                                      <!-- ActionBar styles -->                                                      <style                                                      name                  =                  "MyActionBar"                                                      parent                  =                  "@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"                  >                                                      <                  item name                  =                  "android:background"                  >@                  color                  /                  actionbar_background                  </                  item                  >                                                      <!--                                      Support library compatibility                                    -->                                                      <                  item name                  =                  "background"                  >@                  color                  /                  actionbar_background                  </                  item                  >                                                      </style>                                                      </resources>                              

dan atur warna actionbar yang Anda inginkan

res/values/colors.xml

                                  <?                  xml version                  =                  "1.0"                                      encoding                  =                  "utf-8"                  ?>                                                      <resources>                                                      <color                                                      name                  =                  "actionbar_background"                  >                  #fff                  </color>                                      //write the color you want here                                    </resources>                              

Warna actionbar juga dapat didefinisikan dalam .classfile, potongannya

                                  ActionBar                                      bar                                    =                                      getActionBar                  ();                                      bar                  .                  setBackgroundDrawable                  (                  new                                                      ColorDrawable                  (                  Color                  .                  parseColor                  (                  "#0000ff"                  )));                              

tetapi ini tidak akan bekerja dengan API <11 , jadi menata actionbar di xmlhanya cara untuk API <11





Langsung tambahkan baris ini dengan kode warna Anda

                                  getSupportActionBar                  ().                  setBackgroundDrawable                  (                                                      new                                                      ColorDrawable                  (                  Color                  .                  parseColor                  (                  "#5e9c00"                  )));                              

tidak perlu membuat objek ActionBar setiap kali ...



Pada Nexus 4 orang ini tampaknya membuat warna menjadi abu-abu.

                                  ActionBar                                      bar                                    =                                      getActionBar                  ();                                                      // or MainActivity.getInstance().getActionBar()                                      bar                  .                  setBackgroundDrawable                  (                  new                                                      ColorDrawable                  (                  0xff00DDED                  ));                                      bar                  .                  setDisplayShowTitleEnabled                  (                  false                  );                                                      // required to force redraw, without, gray color                                      bar                  .                  setDisplayShowTitleEnabled                  (                  true                  );                              

(semua kredit untuk posting ini, tetapi terkubur dalam komentar, jadi saya ingin memunculkannya di sini) https://stackoverflow.com/a/17198657/1022454



coba satu kode baris:

                                  getActionBar                  ().                  setBackgroundDrawable                  (                  new                                                      ColorDrawable                  (                  getResources                  ().                  getColor                  (                  R                  .                  color                  .                  MainColor                  )));                              




Gunakan kode di bawah ini untuk memberikan warna berbeda pada teks actionbar dan latar belakang actionbar, gunakan saja tema di bawah ini secara nyata terhadap aktivitas yang Anda inginkan :)

                              <style                                                name                =                "MyTheme"                                                parent                =                "@android:style/Theme.Holo.Light"                >                                                <                item name                =                "android:actionBarStyle"                >@                style                /                MyActionBar                </                item                >                                                </style>                                                <style                                                name                =                "MyActionBar"                                                parent                =                "@android:style/Widget.Holo.Light.ActionBar"                >                                                <                item name                =                "android:titleTextStyle"                >@                style                /                TitleBarTextColor                </                item                >                                                <                item name                =                "android:background"                >                YOUR_COLOR_CODE                </                item                >                                                </style>                                                <style                                                name                =                "TitleBarTextColor"                                                parent                =                "@style/TextAppearance.AppCompat.Widget.ActionBar.Title"                >                                                <                item name                =                "android:textColor"                >                YOUR_COLOR_CODE                </                item                >                                                </style>                          

Ini adalah bagaimana Anda dapat mengubah warna Action Bar.

                              import                                  android                .                app                .                Activity                ;                                                import                                  android                .                content                .                Context                ;                                                import                                  android                .                graphics                .                Color                ;                                                import                                  android                .                graphics                .                drawable                .                ColorDrawable                ;                                                import                                  android                .                os                .                Build                ;                                                import                                  android                .                support                .                v4                .                content                .                ContextCompat                ;                                                import                                  android                .                support                .                v7                .                app                .                ActionBar                ;                                                import                                  android                .                support                .                v7                .                app                .                AppCompatActivity                ;                                                public                                                class                                                ActivityUtils                                                {                                                public                                                static                                                void                                  setActionBarColor                (                AppCompatActivity                                  appCompatActivity                ,                                                int                                  colorId                ){                                                ActionBar                                  actionBar                                =                                  appCompatActivity                .                getSupportActionBar                ();                                                ColorDrawable                                  colorDrawable                                =                                                new                                                ColorDrawable                (                getColor                (                appCompatActivity                ,                                  colorId                ));                                  actionBar                .                setBackgroundDrawable                (                colorDrawable                );                                                }                                                public                                                static                                                final                                                int                                  getColor                (                Context                                  context                ,                                                int                                  id                )                                                {                                                final                                                int                                  version                                =                                                Build                .                VERSION                .                SDK_INT                ;                                                if                                                (                version                                >=                                                23                )                                                {                                                return                                                ContextCompat                .                getColor                (                context                ,                                  id                );                                                }                                                else                                                {                                                return                                  context                .                getResources                ().                getColor                (                id                );                                                }                                                }                                                }                          

Dari MainActivity.java Anda, ubah warna bilah tindakan seperti ini

                                                              ActivityUtils                .                setActionBarColor                (                this                ,                                  R                .                color                .                green_00c1c1                );                          

Ketika Anda Memperluas Aktivitas, gunakan Kode berikut

                              ActionBar                                  actionbar                                =                                  getActionBar                ();                                  actionbar                .                setBackgroundDrawable                (                new                                                ColorDrawable                (                "color"                ));                          

Saat Anda Memperluas AppCompatActivity gunakan Kode berikut

                              ActionBar                                  actionbar                                =                                  getSupportActionBar                ();                                  actionbar                .                setBackgroundDrawable                (                new                                                ColorDrawable                (                "color"                ));                          

Kode ini mungkin bermanfaat

                              <!-- Base application theme. -->                                                <style                                                name                =                "AppTheme"                                                parent                =                "Theme.AppCompat.Light"                >                                                <!--                                  Customize your theme here                .                                                -->                                                </style>                                                <style                                                name                =                "Theme.MyTheme"                                                parent                =                "Theme.AppCompat.Light.DarkActionBar"                >                                                <!--                                  customize the color palette                                -->                                                <                item name                =                "colorPrimary"                >@                color                /                material_blue_500                </                item                >                                                <                item name                =                "colorPrimaryDark"                >@                color                /                material_blue_700                </                item                >                                                <                item name                =                "colorAccent"                >@                color                /                material_blue_500                </                item                >                                                <                item name                =                "colorControlNormal"                >@                color                /                black                </                item                >                                                </style>                                                <style                                                name                =                "CardViewStyle"                                                parent                =                "CardView.Light"                >                                                <                item name                =                "android:state_pressed"                >@                color                /                material_blue_700                </                item                >                                                <                item name                =                "android:state_focused"                >@                color                /                material_blue_700                </                item                >                                                <!--                <                item name                =                "android:background"                >?                android                :                attr                /                selectableItemBackground                </                item                >-->                                                </style>                          

Gunakan kode ini .. untuk mengubah warna latar belakang bilah tindakan. buka "res / values ​​/ themes.xml" (jika tidak ada, buat itu) dan tambahkan

                              <?                xml version                =                "1.0"                                  encoding                =                "utf-8"                ?>                                                <resources>                                                <!-- the theme applied to the application or activity -->                                                <style                                                name                =                "CustomActionBarTheme"                                                parent                =                "@android:style/Theme.Holo.Light.DarkActionBar"                >                                                <                item name                =                "android:actionBarStyle"                >@                style                /                MyActionBar                </                item                >                                                </style>                                                <!-- ActionBar styles -->                                                <style                                                name                =                "MyActionBar"                                                parent                =                "@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"                >                                                <                item name                =                "android:background"                >@                drawable                /                actionbar_background                </                item                >                                                </style>                          

Catatan: kode ini hanya berfungsi untuk Android 3.0 dan versi yang lebih tinggi


Ini bekerja untuk saya, untuk AppCompatActivity,

                                                              <item                                                name                =                "actionBarStyle"                >                @style/BlackActionBar                </item>                                                </style>                                                <style                                                name                =                "BlackActionBar"                                                parent                =                "@style/Widget.AppCompat.ActionBar.Solid"                >                                                <                item name                =                "background"                >@                android                :                color                /                black                </                item                >                                                <                item name                =                "titleTextStyle"                >@                style                /                BlackActionBarTitleTextStyle                </                item                >                                                </style>                                                <style                                                name                =                "BlackActionBarTitleTextStyle"                                                parent                =                "@style/TextAppearance.AppCompat.Widget.ActionBar.Title"                >                                                <                item name                =                "android:textColor"                >@                android                :                color                /                white                </                item                >                                                <                item name                =                "android:fontFamily"                >@                font                /                cinzel_decorative                </                item                >                                                </style>                          

Ini sangat sederhana bagi mereka yang menggunakan API 21 atau lebih tinggi. Gunakan kode ini di bawah ini

untuk Dark ActionBar

                              <resources>                                                <style                                                name                =                "AppTheme"                                                parent                =                "Theme.AppCompat.NoActionBar"                >                                                <!--                                  Customize your theme here                .                                                -->                                                <                item name                =                "android:background"                >@                color                /                colorDarkGrey                </                item                >                                                </style>                          

for_LightActionBar

                              <resources>                                                <style                                                name                =                "AppTheme"                                                parent                =                "Theme.AppCompat.Light.ActionBar"                >                                                <                item name                =                "android:background"                >@                color                /                colorDarkGrey                </                item                >                                                </style>                          

Gunakan ini, itu akan berhasil.

                              ActionBar                                  actionbar                                =                                  getActionBar                ();                                  actionbar                .                setBackgroundDrawable                (                new                                                ColorDrawable                (                "color"                ));                          

Terkadang opsi ini melempar NullPointerException

ActionBar actionbar = getActionBar(); actionbar.setBackgroundDrawable(new ColorDrawable("color"));

Tetapi opsi ini berhasil bagi saya. Jadi kamu bisa coba ini.

                              getSupportActionBar                ().                setBackgroundDrawable                (                new                                                ColorDrawable                (                Color                .                parseColor                (                "#FFFFFF"                )));                          

Selamat Coding


Jika Anda menggunakan Androidx AppCompact. Gunakan kode di bawah ini.

                              androidx                .                appcompat                .                app                .                ActionBar                                  actionBar                                =                                  getSupportActionBar                ();                                  actionBar                .                setBackgroundDrawable                (                new                                                ColorDrawable                (                "Color"                ));                          

                              getActionBar                ().                setBackgroundDrawable                (                new                                                ColorDrawable                (                getResources                ().                getColor                (                R                .                color                .                TabColor                )));                          

file color.xml:

                              <resources>                                                <color                                                name                =                "TabColor"                >                #11FF00                </color>                                                </resources>                `                          

How To Change Action Bar Title Color In Android

Source: https://qastack.id/programming/8024706/how-do-i-change-the-background-color-of-the-actionbar-of-an-actionbaractivity-us

Posted by: pereabeemsty1980.blogspot.com

0 Response to "How To Change Action Bar Title Color In Android"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel