IT

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

lottoking 2020. 9. 17. 08:08
반응형

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


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

탑 블랙

난 이런 걸 원해요

탑 레드

어떻게 할 수 있습니까?


사용자 정의 스타일만들어 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의 액션 바 배경색을 어떻게 변경합니까?


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

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

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

여기에 이미지 설명 입력

  1. AppCompat 다크 테마 (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 :-

이를 위해 안드로이드 액션 바 UI 요소에 대한 스타일 특성을 보유하는 @ 스타일 / 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 옵션 메뉴 스타일은 이 링크를 참조하십시오.


Android 3.0 이상 전용

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

입술 / 값 / 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


다음과 같이 작업 표시 줄 색상을 설명 수 있습니다.

<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 사이트에서 가져온 스크린 샷이며 색상 팔레트 사용자 지정에 대한 자세한 내용을 읽을 수 있는 링크입니다.

여기에 이미지 설명 입력


또 다른 가능성.

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


사용자 정의 색상 :

 <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


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


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>

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

몇 분 안에 실시간 미리보기로 액션 바를 맞춤 설정할 수있는 좋은 도구입니다.

참고 URL : https://stackoverflow.com/questions/18288402/how-to-set-custom-actionbar-color-style

반응형