2015-04-06 30 views
20

Tôi đang cố gắng để thay đổi màu sắc thanh công cụ Background lập trình bằng cách làm này:Thay đổi Toolbar lập trình không thay đổi Toolbar Tiêu đề Màu nền

getSupportActionBar().setBackgroundDrawable(newColorDrawable(getResources().getColor(R.color.test_color_blue))); 

Và đây là kết quả:

trước:

enter image description here

Sau:

enter image description here

Một số cách tiêu đề thanh công cụ vẫn có cùng màu nền như trước.

đây là thanh công cụ của tôi 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:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center_vertical" 
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
app:theme="@style/Theme.Toolbar"> 

Và đây là Theme:

<style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item> 
    <item name="android:background">@color/primary</item> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@android:color/white</item> 
    <item name="titleTextAppearance">@style/Theme.Toolbar.Title</item> 
</style> 
+0

Bạn đã áp dụng bất kỳ kiểu tùy chỉnh nào cho thanh tác vụ trong tệp styles.xml, có thể một số trong số đó là mâu thuẫn. – Harry

+0

đăng bố cục xml của bạn, vui lòng –

+0

@DavidJhons tôi đã đăng chi tiết mới về các câu hỏi – EkKoZ

Trả lời

4

này sử dụng để truy cập TextView

public void changeToggleTitle() { 
    if (mToolbar != null) { 
     for(int i= 0; i < mToolbar.getChildCount(); i++){ 
      View v = mToolbar.getChildAt(i); 
      if(v != null && v instanceof TextView){ 
       TextView t = (TextView) v; 
       // Do the magic 
      } 
     } 
    } 
} 
12

Thay đổi mã của bạn như sau:

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     style="@style/MyToolbarStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical"> 

Theme/Phong cách

<style name="MyToolbarStyle"> 
    <item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item> 
    <item name="android:background">@color/primary</item> 
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item> 
    <item name="titleTextAppearance">@style/Theme.Toolbar.Title</item> 
    <!-- No need for colorPrimary, colorPrimaryDark, colorAccent here 
     this should go to the AppTheme --> 
</style> 

quả

Trước s Etting màu nền mới:

picture before background color change

và sau:

picture after background color change

+0

Bạn đã đặt colorPrimary/PrimaryDark ở đâu, v.v. nơi bạn đặt 'AppTheme'? Ngoài ra, có lý do cụ thể nào mà bạn không đặt chủ đề gốc không? – ThePersonWithoutC

+1

Về cơ bản nó chỉ là một phong cách tùy chỉnh và tôi không muốn kế thừa bất kỳ thuộc tính nào từ một chủ đề khác, do đó tôi không chỉ định một chủ đề gốc. Về 'colorPrimary': Nó được thiết lập trong AppTheme được ứng dụng/hoạt động sử dụng. Theo mặc định, 'Thanh công cụ' đặt nền tùy thuộc vào thuộc tính 'colorPrimary' trừ khi bạn không ghi đè lên nó bằng một kiểu. – reVerse

4

muộn, nhưng tôi hy vọng nó sẽ là hữu ích bình luận

tôi đã item này trong Hoạt động phong cách của tôi

<item name="android:background">someColor</item> 

vì vậy khi tôi thay đổi toolbar các mục màu, tiêu đề và menu không thay đổi nền. Tôi vừa xóa số này item và giờ nó hoạt động hoàn hảo.

Tôi không có thời gian để hiểu chi tiết, nhưng tôi nghĩ nó có thể hữu ích cho người khác.

0
mToolbar.setBackgroundResource(mGameEnum.getPrimeColorRes()); 
Các vấn đề liên quan