사용자 지정 ActionBar 색상 / 스타일을 설정하는 방법은 무엇입니까?


87

Android Navigation bar프로젝트에서 사용 중입니다. 작업 표시 줄의 상단 색상을 빨간색으로 변경하고 싶습니다. 어떻게해야합니까? 이런 게 있어요

탑 블랙

난 이런 걸 원해요

탑 레드

어떻게 할 수 있습니까?

답변:


174

사용자 정의 스타일 을 만들어 ActionBar (및 기타 항목)의 색상을 정의 할 수 있습니다 .

Android 프로젝트 의 res / values ​​/ styles.xml 파일을 편집하기 만하면 됩니다.

예를 들면 다음과 같습니다.

<resources>
    <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    </style>

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

그런 다음 "MyCustomTheme"를 ActionBar를 포함하는 활동의 테마로 설정하십시오.

다음과 같이 ActionBar의 색상을 설정할 수도 있습니다.

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED)); // set your desired color

여기에서 가져온 것 : XML을 사용하여 ActionBarActivity의 ActionBar 배경색을 어떻게 변경합니까?


2
"그런 다음"MyCustomTheme "를 ActionBar를 포함하는 활동의 테마로 설정하십시오."라는 의미는 무엇입니까? 어떻게하나요?
— Kala J

2
활동 XML 에서 <android.support.v4.view.ViewPager ... /> 태그 안에 android : theme = "@ style / MyCustomTheme" 를
— 매개 변수로

2
무엇 안드로이드에 valuesv14 및 valuesv21 styles.xml 파일을 나머지에 대한
— Harsha에

제목 textStyle을 변경하는 방법은 무엇입니까?
— Adam Hurwitz

@KalaJ는 setTheme(R.style.MyCustomTheme) 활동의 onCreate 메소드에서 사용합니다.
— NullByte08

57

일반적으로 Android OS는 '테마'를 활용하여 앱 개발자가 범용 UI 요소 스타일링 매개 변수 집합을 Android 애플리케이션에 전체적으로 적용하거나 단일 활동 하위 클래스에 적용 할 수 있도록합니다.

따라서 버전 3.0 이상 버전 용 앱을 개발할 때 Android Manifest XML 파일에 지정할 수있는 세 가지 주요 Android OS '시스템 테마'가 있습니다.

여기에서 (APPCOMPAT) 지원 라이브러리를 참조하고 있습니다 .-- 따라서 세 가지 테마는 1. AppCompat Light 테마 (Theme.AppCompat.Light)입니다.

여기에 이미지 설명 입력

  1. AppCompat Dark Theme (Theme.AppCompat),

여기에 이미지 설명 입력

  1. 그리고이 두 가지 AppCompat Light 테마와 Darker ActionBar의 하이브리드입니다. (Theme.AppCompat.Light.DarkActionBar)

AndroidManifest.xml 태그를 보면 Android 테마가 다음과 같이 언급됩니다 .-- android : theme = "@ style / AppTheme"

Styles.xml을 열고 거기에 선언 된 기본 애플리케이션 테마가 있습니다.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  </style>

액션 바의 스타일을 지정하려면 이러한 상위 테마 요소를 재정의해야합니다.

다른 색상 배경의 ActionBar :-

이렇게하려면 Android ActionBar UI 요소의 스타일 특성을 보유하는 @ style / Widget.AppCompat.Light.ActionBar.Solid.Inverse에 대한 상위 참조를 사용하여 새 스타일 MyActionBar (모든 이름을 지정할 수 있음)를 만들어야 합니다. 그래서 정의는

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

그리고이 정의는 AppTheme에서 참조해야하며 재정의 된 ActionBar 스타일을 다음과 같이 가리 킵니다.

@ 스타일 / MyActionBar 분홍색 배경색의 ActionBar

제목 표시 줄 텍스트 색상 변경 (예 : 검은 색에서 흰색으로) :-

이제 제목 텍스트 색상을 변경하려면 상위 참조 parent = "@ style / TextAppearance.AppCompat.Widget.ActionBar.Title"> 을 재정의해야합니다.

따라서 스타일 정의는

<style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
</style>

TitleTextStyle 수정은 ActionBar 부모 OS UI 요소의 자식 요소이기 때문에 MyActionBar 스타일 정의 내에서이 스타일 정의를 참조합니다 . 따라서 MyActionBar 스타일 요소의 최종 정의는

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

그래서 이것은 최종 Styles.xml입니다.

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- This is the styling for action bar -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

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

    <style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
    </style>
</resources>

흰색 제목 색상의 ActionBar 추가 ActionBar 옵션 메뉴 스타일은 이 링크를 참조하십시오.


2
이봐, 작동 했어. 메뉴 오버플로 아이콘의 색상을 변경하려면 테마에 따라 색상이 바뀝니다. 어두운 테마의 흰색 아이콘과 밝은 테마의 어두운 아이콘, 흰색으로 변경하려면 어떻게해야합니까? 답변의 마지막 이미지를 고려하십시오.
— Ameen Maheen

33

Android 3.0 이상 전용

Android 3.0 이상 만 지원하는 경우 다음과 같이 작업 표시 줄의 배경을 정의 할 수 있습니다.

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.Holo.Light.DarkActionBar">
       <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

<!-- ActionBar styles -->
  <style name="MyActionBar" parent="@style/Widget.Holo.Light.ActionBar.Solid.Inverse">
       <item name="android:background">#ff0000</item>
  </style>
</resources>

Android 2.1 이상

지원 라이브러리를 사용할 때 스타일 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">@drawable/actionbar_background</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_background</item>
  </style>
 </resources>

그런 다음 전체 앱 또는 개별 활동에 테마를 적용합니다.

자세한 내용은 Documentaion


이 이상하다 : 당신이 2.1 이상 (재료 설계 / APPCOMPAT)보다 안드로이드 3.0 이상 이전 스타일 (홀로그램)을 얻었다 ...
— 로엘

@DalvikVM 예 .. Appcompat는 이전 버전 용입니다. 더 높은 버전에 사용할 필요가 없습니다. 이 답변 당시 재료 디자인은 도입되지 않았습니다.
— 케탄 아히

오류가 발생합니다. No resource found that matches the given name '@style/Widget.Holo.Light.ActionBar.Solid.Inverse'.변경하면 android:style/Widget.Holo.Light.ActionBar.Solid.Inverse문제가 해결됩니다. 감사.
— Annie Lagang 2015

@ style / Theme.AppCompat.Light.DarkActionBar는 내 경우 (Android Studio)에서 해결할 수 없습니다-@android : style / Theme.AppCompat.Light.DarkActionBar-즉 스타일 앞에 "android"가 추가되어 있지 않아야합니까?
— AgentKnopf

32

다음과 같이 작업 표시 줄 색상을 변경할 수 있습니다.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/green_action_bar</item>
</style>

액션 바 색상을 변경하는 데 필요한 모든 것입니다.

또한 상태 표시 줄 색상을 변경하려면 다음 줄을 추가하십시오.

 <item name="android:colorPrimaryDark">@color/green_dark_action_bar</item>

다음은 개발자 Android 사이트에서 가져온 스크린 샷이며 색상 팔레트 사용자 지정에 대한 자세한 내용을 읽을 수 있는 링크입니다.

여기에 이미지 설명 입력


7

또 다른 가능성.

actionBar.setBackgroundDrawable (new ColorDrawable (Color.parseColor ( "# 0000ff")));


5

titleTextColor를 사용하여 ActionBar 텍스트 색상을 변경할 수 있습니다.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="titleTextColor">#333333</item>
</style>

3

사용자 정의 색상 :

 <style name="AppTheme" parent="Theme.AppCompat.Light">

                <item name="colorPrimary">@color/ColorPrimary</item>
                <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
                <!-- Customize your theme here. -->
     </style>

맞춤 스타일 :

 <style name="Theme.AndroidDevelopers" parent="android:style/Theme.Holo.Light">
        <item name="android:selectableItemBackground">@drawable/ad_selectable_background</item>
        <item name="android:popupMenuStyle">@style/MyPopupMenu</item>
        <item name="android:dropDownListViewStyle">@style/MyDropDownListView</item>
        <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
        <item name="android:actionDropDownStyle">@style/MyDropDownNav</item>
        <item name="android:listChoiceIndicatorMultiple">@drawable/ad_btn_check_holo_light</item>
        <item name="android:listChoiceIndicatorSingle">@drawable/ad_btn_radio_holo_light</item>
    </style>

추가 정보 : Android ActionBar


2

AppCompatActivity위의 답변 을 사용 했기 때문에 나를 위해 일하지 않았습니다. 그러나 아래 솔루션이 작동했습니다.

에서 고해상도 / styles.xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
</style>


추신 : colorPrimary대신 사용 했습니다android:colorPrimary


1

style.xml에서이 코드를 추가하십시오.

<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyAction</item>
    </style>

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#333333</item>
    </style>
</resources>

0

이것을 사용하십시오 -http : //jgilfelt.github.io/android-actionbarstylegenerator/

몇 분 안에 실시간 미리보기로 작업 표시 줄을 사용자 지정할 수있는 좋은 도구입니다.


1
이 링크가 질문에 답할 수 있지만 여기에 답변의 필수 부분을 포함하고 참조 용 링크를 제공하는 것이 좋습니다. 링크 된 페이지가 변경되면 링크 전용 답변이 무효화 될 수 있습니다.
— Roman Marusyk
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.