2013-07-17 48 views
5

Bất kể cách tôi sửa đổi kiểu, màu nền của hộp được trỏ đến bằng mũi tên màu đỏ sẽ không thay đổi từ nền.Làm cách nào để thay đổi màu nền của tiêu đề ActionBar?

img

Làm cách nào để thay đổi màu đó? Tôi đang nhắm mục tiêu 4.0+ và không quan tâm đến 4.0 thiết bị và hệ điều hành.

Đây là liên kết đến my styles.xml file. Xin lỗi tôi đã phải kiểm duyệt một vài điều khi dự án này thuộc NDA.

Tôi đã thử nhiều thứ bao gồm:

  • Thay đổi android:background trong windowTitleBackgroundStyle
  • Thay đổi android:background trong actionBarStyle
  • Thay đổi android:background trong actionBarWidgetStyle
  • Thay đổi android:actionBarItemBackground

Không có tác phẩm nào trong số đó. Chế độ xem văn bản đó dường như luôn gắn bó với giá trị android:background từ chủ đề cơ sở của tôi và không có gì có thể ghi đè nó.

Tôi đang thiếu hoặc làm gì sai?

+0

Sử dụng http://jgilfelt.github.io/android-actionbarstylegenerator để tạo một bộ tài nguyên và xem những gì có thể có liên quan mà bạn chưa thử. – CommonsWare

Trả lời

1

Có phần interisting file xml của bạn:

<!-- ActionBar --> 
    <style name="CCCWidgetActionBar" parent="@android:style/Widget.Holo.ActionBar"> 
     <item name="android:background">@color/nav_background</item> 
     <item name="android:textColor">@color/nav_text</item> 
     <item name="android:titleTextStyle">@style/CCCWidgetActionBarTitle</item> 
     <item name="android:textViewStyle">@style/CCCWidgetActionBarTitle</item> 

     <item name="android:windowTitleStyle">@style/CCCWindowTitle</item> 
     <item name="android:windowTitleBackgroundStyle">@style/CCCWindowTitleBackground</item> 
     <item name="android:windowBackground">@color/nav_background</item> 
    </style> 

Chỉ cần thay đổi:

<item name="android:background">@color/nav_background</item> 

By

<item name="android:background">@color/pink</item> 

Hoặc bất kỳ màu nào khác.

Làm việc tốt cho tôi.

+0

Không hoạt động trên Galaxy S II của tôi. Cùng một vấn đề như đã nêu trong câu hỏi. – chakrit

+0

Văn bản tiêu đề không sử dụng nền thanh tác vụ, chỉ có kiểu nền ứng dụng. Điều này làm cho nó không thể thay đổi một màu cụ thể mà không làm thay đổi màu nền của mọi thứ khác. – chakrit

+0

Tôi cũng gặp vấn đề tương tự trên Lenovo S880. Tôi nghi ngờ có thể đây là vấn đề thiết bị, tuy nhiên ứng dụng Google+ có màu sắc trên thanh công cụ tuyệt vời trên cùng một thiết bị .. lạ – Zyoo

1

bạn tạo Thanh tác vụ tùy chỉnh và có thể thay đổi Màu nền tiêu đề, bất kể bạn muốn gì.

Thực hiện theo các bước sau:

Chỉ có thể thực hiện điều này để làm cho Custom Sherlock bar for Title.

tạo tệp xml riêng biệt cho tiêu đề trên Ứng dụng.

rằng tập tin chứa như thế này ..

Trong bố trí tùy chỉnh này, chúng ta có thể có khả năng thay đổi màu nền sử dụng mã màu ..

header_sherlock.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
android:weightSum="3" 
android:background="#008000" 
> 

    <TextView 
    android:id="@+id/header_tvleft" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="left" 
    android:layout_marginTop="@dimen/margintop" 
    android:clickable="true" 
    android:paddingLeft="@dimen/layoutpaddingleft" 
    android:text="Textview" 
    android:textColor="#ffffff" 
    android:textSize="@dimen/header_leftbtn" 
    /> 


<TextView 
    android:id="@+id/header_tvcenter" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_horizontal" 
    android:layout_marginTop="@dimen/margintop" 
    android:layout_marginRight="@dimen/sherlockpaddingright" 
    android:layout_marginLeft="@dimen/sherlockpaddingleft" 
    android:text="Textview" 
    android:textColor="#ffffff" 
    android:textStyle="bold" 
    android:textSize="@dimen/header_title" 
    /> 

    <TextView 
    android:id="@+id/header_tvright" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="right" 
    android:layout_marginTop="@dimen/margintop" 
    android:clickable="true" 
    android:paddingRight="@dimen/layoutpaddingright" 
    android:text="Textview" 
    android:textColor="#ffffff" 
    android:textSize="@dimen/header_rightbtn" 
    /> 

</LinearLayout> 

Đối MainActivity hoặc hoạt động nào bạn muốn thêm tiêu đề:

  getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setCustomView(R.layout.header_sherlock); 
    header_tvleft = (TextView) findViewById(R.id.header_tvleft); 
    header_tvleft.setText("LeftTitleName"); 
    header_tvcenter = (TextView) findViewById(R.id.header_tvcenter); 
    header_tvcenter.setText("CenterTitleName"); 
    header_tvright = (TextView) findViewById(R.id.header_tvright); 
    header_tvright.setText("RightTitleName"); 

Thử thi s Phương pháp ..

+0

Có thể thực hiện điều này mà không cần sử dụng ActionBarSherlock và chỉ cần lưu trữ Android ActionBar? – chakrit

0

Đây là một câu hỏi cũ nhưng tôi vừa mới chính xác cùng một vấn đề - đối với tôi vấn đề là tôi có điều này:

<item name="android:actionBarItemBackground">@drawable/btn_dark</item> 
<item name="actionBarItemBackground">@drawable/btn_dark</item> 

đâu btn_dark là một drawable tái sử dụng cho các nút chung xác định một màu cho trạng thái đã bật, nhấn và tắt. Nó hoạt động tốt trên các thiết bị mới hơn nhưng trên điện thoại 2.3 cũ của tôi, tôi đã nhận được một nền màu xám trên tiêu đề.

Điều thú vị là trạng thái vô hiệu hóa (mà tôi đã đặt là màu xám đậm), được áp dụng trên các thiết bị cũ hơn vào nền của tiêu đề thanh tác vụ. Đặt màu bị tắt thành trong suốt là bản sửa lỗi.

4

Vâng, bạn cần phải thay đổi android: background với windowBackground

<style name="CCCBaseTheme" parent="@android:style/Theme.Holo"> 
    <item name="android:windowBackground">@color/nav_background</item> 
    <item name="android:textColor">@color/text</item> 

    <item name="android:actionBarStyle">@style/CCCWidgetActionBar</item> 

</style> 

hy vọng điều này sẽ giúp.

+0

Wow, sau 2 năm haha. Tôi không chắc đây có phải là giải pháp đúng vì tôi không thể thử nghiệm nó trong ngữ cảnh của câu hỏi được nữa. Nếu người khác đến và nói điều này hoạt động, tôi sẽ đánh dấu là được chấp nhận. – chakrit

+0

hehe hôm qua tôi phải đối mặt với vấn đề này tôi luôn luôn sử dụng thanh hành động tùy chỉnh: S .... Tôi đã thử nghiệm nó (windowBackground) và nó hoạt động tốt :) –