Programming

새 툴바의 제목 색상은 어떻게 설정합니까?

procodes 2020. 8. 8. 14:14
반응형

새 툴바의 제목 색상은 어떻게 설정합니까?


저는 새로운 v7 툴바를 사용하고 있으며 평생 동안 제목의 색상을 변경하는 방법을 알 수 없습니다. Toolbar의 @style을 styles.xml에 선언 된 스타일로 설정하고 textColor와 함께 titleTextStyle을 적용했습니다. 뭔가 빠졌습니까? Lollipop 용으로 코딩 중이지만 현재 Kitkat 장치에서 테스트 중입니다.

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="ActionBar" parent="Theme.AppCompat">
        <item name="android:background">@color/actionbar_background</item>
        <item name="android:titleTextStyle">@style/ActionBar.TitleTextStyle</item>
    </style>

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

</resources>

actionbar.xml :

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_actionbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    style="@style/ActionBar"/>

옵션 1) 빠르고 쉬운 방법 (도구 모음 만 해당)

appcompat-v7-r23 이후로 다음 속성을 사용자 Toolbar또는 해당 스타일 에 직접 사용할 수 있습니다 .

app:titleTextColor="@color/primary_text"
app:subtitleTextColor="@color/secondary_text"

최소 SDK가 23이고 네이티브를 사용 Toolbar하는 경우 네임 스페이스 접두사를 android.

Java에서는 다음 방법을 사용할 수 있습니다.

toolbar.setTitleTextColor(Color.WHITE);
toolbar.setSubtitleTextColor(Color.WHITE);

이 메서드는 색상 리소스 ID가 아닌 색상 int를 사용합니다!

옵션 2) 툴바 스타일 및 테마 속성 재정의

레이아웃 /xxx.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.MyApp.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    style="@style/Widget.MyApp.Toolbar.Solid"/>

values ​​/ styles.xml

<style name="Widget.MyApp.Toolbar.Solid" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@color/actionbar_color</item>
    <item name="android:elevation" tools:ignore="NewApi">4dp</item>
    <item name="titleTextAppearance">...</item>
</style>

<style name="ThemeOverlay.MyApp.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
    <!-- Parent theme sets colorControlNormal to textColorPrimary. -->
    <item name="android:textColorPrimary">@color/actionbar_title_text</item>
</style>

도움! 내 아이콘도 색상이 변경되었습니다!

@PeterKnut은 이것이 오버플로 버튼, 탐색 창 버튼 및 뒤로 버튼의 색상에 영향을 미친다고보고했습니다. 또한의 텍스트 색상을 변경합니다 SearchView.

아이콘 색상 관련 : 다음에서 colorControlNormal상속

  • android:textColorPrimary 어두운 테마 용 (검정 바탕에 흰색)
  • android:textColorSecondary 밝은 테마 (흰색 바탕에 검은 색)

이것을 액션 바의 테마에 적용 하면 아이콘 색상을 사용자 지정할 수 있습니다.

<item name="colorControlNormal">#de000000</item>

appcompat-v7에서 r23까지의 버그가 있었기 때문에 다음과 같이 기본 대응 항목도 재정의해야합니다.

<item name="android:colorControlNormal" tools:ignore="NewApi">?colorControlNormal</item>

도움! 내 SearchView가 엉망입니다!

참고 :이 섹션은 더 이상 사용되지 않을 수 있습니다.

어떤 이유로 appcompat-v7에 포함 된 것과 다른 뒤로 화살표 (시각적, 기술적으로 아님)를 사용하는 검색 위젯을 사용하기 때문에 앱의 테마 에서 수동으로 설정해야합니다 . 지원 라이브러리의 드로어 블이 올바르게 착색됩니다. 그렇지 않으면 항상 흰색이됩니다.

<item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>

검색보기 텍스트에 관해서는 ... 쉬운 방법은 없습니다. 소스를 살펴본 후 텍스트보기로 이동하는 방법을 찾았습니다. 나는 이것을 테스트하지 않았으므로 이것이 작동하지 않으면 의견에 알려주십시오.

SearchView sv = ...; // get your search view instance in onCreateOptionsMenu
// prefix identifier with "android:" if you're using native SearchView
TextView tv = sv.findViewById(getResources().getIdentifier("id/search_src_text", null, null));
tv.setTextColor(Color.GREEN); // and of course specify your own color

보너스 : ActionBar 스타일 및 테마 속성 재정의

기본 작업 appcompat-v7 작업 표시 줄에 대한 적절한 스타일은 다음과 같습니다.

<!-- ActionBar vs Toolbar. -->
<style name="Widget.MyApp.ActionBar.Solid" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="background">@color/actionbar_color</item> <!-- No prefix. -->
    <item name="elevation">4dp</item> <!-- No prefix. -->
    <item name="titleTextStyle">...</item> <!-- Style vs appearance. -->
</style>

<style name="Theme.MyApp" parent="Theme.AppCompat">
    <item name="actionBarStyle">@style/Widget.MyApp.ActionBar.Solid</item>
    <item name="actionBarTheme">@style/ThemeOverlay.MyApp.ActionBar</item>
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
</style>

검색 위젯에서 텍스트 색상이 아닌 제목 색상 만 변경해야하는 경우 여기에 내 솔루션이 있습니다.

layout / toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:background="@color/toolbar_bg"
    app:theme="@style/AppTheme.Toolbar"
    app:titleTextAppearance="@style/AppTheme.Toolbar.Title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"/>

values ​​/ themes.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
    </style>

    <style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.ActionBar">
        <!-- Customize color of navigation drawer icon and back arrow --> 
        <item name="colorControlNormal">@color/toolbar_icon</item>
    </style>

    <style name="AppTheme.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
        <!-- Set proper title size -->
        <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item>
        <!-- Set title color -->
        <item name="android:textColor">@color/toolbar_title</item>
    </style>
</resources>

비슷한 방법으로 subtitleTextAppearance도 설정할 수 있습니다.


API 23 이상을 지원하는 경우 이제 titleTextColor 속성 을 사용하여 툴바의 제목 색상을 설정할 수 있습니다 .

layout / toolbar.xml

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:titleTextColor="@color/colorPrimary"
        />

MyActivity.java

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar)
toolbar.setTitleTextColor(Color.WHITE);

Android 4.4 및 6.0에서도 app:titleTextColorandroid.support.v7.widget.Toolbar작품 설정 com.android.support:appcompat-v7:23.1.0:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:titleTextColor="@android:color/white" />

이것은 나를 괴롭 혔고 주어진 답변이 마음에 들지 않아 소스를 살펴보고 어떻게 작동하는지 확인했습니다.

FractalWrench는 올바른 경로에 있지만 API 23 이하에서 사용할 수 있으며 도구 모음에서 자체적으로 설정할 필요가 없습니다.

다른 사람들이 말했듯이 도구 모음에서 스타일을 설정할 수 있습니다.

app:theme="@style/ActionBar"

이 스타일에서 제목 텍스트 색상을 설정할 수 있습니다.

<item name="titleTextColor">#00f</item>

API 23 이전 또는 23+

<item name="android:titleTextColor">your colour</item>

전체 XML

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    app:theme="@style/ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>


<style name="ActionBar" parent="@style/ThemeOverlay.AppCompat.ActionBar">
    <item name="android:titleTextStyle">@style/ActionBarTextStyle</item>
    <item name="titleTextColor">your colour</item>
    <item name="android:background">#ff9900</item>
</style>

툴바에 설정할 수있는 모든 속성이 있습니다.

<declare-styleable name="Toolbar">
    <attr name="titleTextAppearance" format="reference" />
    <attr name="subtitleTextAppearance" format="reference" />
    <attr name="title" />
    <attr name="subtitle" />
    <attr name="gravity" />
    <attr name="titleMargins" format="dimension" />
    <attr name="titleMarginStart" format="dimension" />
    <attr name="titleMarginEnd" format="dimension" />
    <attr name="titleMarginTop" format="dimension" />
    <attr name="titleMarginBottom" format="dimension" />
    <attr name="contentInsetStart" />
    <attr name="contentInsetEnd" />
    <attr name="contentInsetLeft" />
    <attr name="contentInsetRight" />
    <attr name="maxButtonHeight" format="dimension" />
    <attr name="navigationButtonStyle" format="reference" />
    <attr name="buttonGravity">
        <!-- Push object to the top of its container, not changing its size. -->
        <flag name="top" value="0x30" />
        <!-- Push object to the bottom of its container, not changing its size. -->
        <flag name="bottom" value="0x50" />
    </attr>
    <!-- Icon drawable to use for the collapse button. -->
    <attr name="collapseIcon" format="reference" />
    <!-- Text to set as the content description for the collapse button. -->
    <attr name="collapseContentDescription" format="string" />
    <!-- Reference to a theme that should be used to inflate popups
         shown by widgets in the toolbar. -->
    <attr name="popupTheme" format="reference" />
    <!-- Icon drawable to use for the navigation button located at
         the start of the toolbar. -->
    <attr name="navigationIcon" format="reference" />
    <!-- Text to set as the content description for the navigation button
         located at the start of the toolbar. -->
    <attr name="navigationContentDescription" format="string" />
    <!-- Drawable to set as the logo that appears at the starting side of
         the Toolbar, just after the navigation button. -->
    <attr name="logo" />
    <!-- A content description string to describe the appearance of the
         associated logo image. -->
    <attr name="logoDescription" format="string" />
    <!-- A color to apply to the title string. -->
    <attr name="titleTextColor" format="color" />
    <!-- A color to apply to the subtitle string. -->
    <attr name="subtitleTextColor" format="color" />
</declare-styleable>

Toolbar에서 제목 색상 을 변경하는 가장 간단한 방법 입니다 CollapsingToolbarLayout.

아래 스타일 추가 CollapsingToolbarLayout

<android.support.design.widget.CollapsingToolbarLayout
        app:collapsedTitleTextAppearance="@style/CollapsedAppBar"
        app:expandedTitleTextAppearance="@style/ExpandedAppBar">

styles.xml

<style name="ExpandedAppBar" parent="@android:style/TextAppearance">
        <item name="android:textSize">24sp</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textAppearance">@style/TextAppearance.Lato.Bold</item>
    </style>

    <style name="CollapsedAppBar" parent="@android:style/TextAppearance">
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textAppearance">@style/TextAppearance.Lato.Bold</item>
    </style>

appcompat-v7 app : titleTextColor = "# fff">를 사용할 수있는 경우

<android.support.v7.widget.Toolbar  
        android:id="@+id/toolbar"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:background="@color/colorPrimary"   
        android:visibility="gone"   
        app:titleTextColor="#fff">   
    </android.support.v7.widget.Toolbar>   

이것은 나를 위해 일했습니다.

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    app:navigationIcon="@drawable/ic_back"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:subtitleTextColor="@color/white"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:title="This week stats"
    app:titleTextColor="@color/white">


    <ImageView
        android:id="@+id/submitEditNote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:layout_marginRight="10dp"
        android:src="@android:drawable/ic_menu_manage" />
</android.support.v7.widget.Toolbar>

xml ... toolbar.xml에 도구 모음을 만듭니다.

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"

    </android.support.v7.widget.Toolbar>

그런 다음 toolbar.xml에 다음을 추가합니다.

app:titleTextColor="@color/colorText"
app:title="@string/app_name">

Remeber @ color / colorText는 colorText라는 색상 속성과 색상을 가진 color.xml 파일입니다. 이것은 toolbar.xml 내부에서 색상을 하드 코딩하는 것보다 문자열을 호출하는 가장 좋은 방법입니다. 다음과 같이 텍스트를 수정할 수있는 다른 옵션도 있습니다. 예 : textAppearance ... etc ... app : text를 입력하면 intelisense가 Android 스튜디오에서 옵션을 제공합니다.

최종 도구 모음은 다음과 같아야합니다.

 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/Theme.AppCompat"
       app:subtitleTextAppearance="@drawable/icon"
        app:title="@string/app_name">
    </android.support.v7.widget.Toolbar>

주의 :이 툴바는 activity_main.xml 안에 있어야합니다 .Easy Peasie

또 다른 옵션은 수업에서 모든 작업을 수행하는 것입니다.

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.WHITE);

행운을 빕니다


For Change The color

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="?attr/colorPrimary"

/>

Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
        myToolbar.setTitleTextColor(ContextCompat.getColor(getApplicationContext(), R.color.Auth_Background));
        setSupportActionBar(myToolbar);

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@color/white"
    android:background="@color/green" />


public class MyToolbar extends android.support.v7.widget.Toolbar {

public MyToolbar(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    AppCompatTextView textView = (AppCompatTextView) getChildAt(0);
    if (textView!=null) textView.setTextAppearance(getContext(), R.style.TitleStyle);
}

}

Or simple use from MainActivity:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarMain);
setSupportActionBar(toolbar);
((AppCompatTextView) toolbar.getChildAt(0)).setTextAppearance(this, R.style.TitleStyle);

style.xml

<style name="TileStyle" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@color/White</item>
    <item name="android:shadowColor">@color/Black</item>
    <item name="android:shadowDx">-1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">1</item>
</style>

Do it with toolbar.setTitleTextAppearance(context, R.style.TitleTextStyle);

//this is style where you can customize your color scheme

 <style name="TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:fontFamily">@string/you_font_family</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textAppearance">@style/you_text_style</item>
    <item name="android:textColor">#000000</item>
    <item name="android:textSize">20sp</item>
</style>

Very simple, this worked for me (title and icon white):

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/PrimaryColor"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:elevation="4dp" />

참고URL : https://stackoverflow.com/questions/26852108/how-do-you-set-the-title-color-for-the-new-toolbar

반응형