2014-10-19 11 views
10

Tôi đang cố gắng tạo chủ đề ứng dụng nhưng màu không hoạt động và biểu tượng sẽ không hiển thị ... có thể là im thiếu thứ gì đó. Đây là xml của tôi:SupportBar sẽ không hiển thị màu phù hợp với API 21

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <!-- the theme applied to the application or activity --> 
    <color name="background_test">#410000</color> 
    <color name="font_general">#ffffff</color> 
    <style name="MyTheme" 
     parent="@style/Theme.AppCompat"> 
     <item name="android:actionBarStyle">@style/MyActionBar</item> 
     <!-- Support library compatibility --> 
     <item name="actionBarStyle">@style/MyActionBar</item> 

    </style> 
    <style name="AudioFileInfoOverlayText"> 
     <item name="android:paddingLeft">2dp</item> 
     <item name="android:paddingBottom">2dp</item> 
     <item name="android:textColor">#ffffffff</item> 
     <item name="android:textSize">18sp</item> 
     <item name="android:shadowColor">#000000</item> 
     <item name="android:shadowDx">1</item> 
     <item name="android:shadowDy">1</item> 
     <item name="android:shadowRadius">1</item> 
    </style> 

    <!-- general styles for the action bar --> 
    <style name="MyActionBar" 
     parent="@style/Widget.AppCompat.Light.ActionBar.Solid"> 
     <item name="android:icon">@drawable/ic_launcher</item> 
     <item name="icon">@drawable/ic_launcher</item> 
     <item name="background">@color/background_test</item> 
     <item name="android:background">@color/background_test</item> 
    </style> 

</resources> 

Tôi nên thêm gì để làm cho nó hoạt động?

EDIT:

Nó sẽ không làm việc với một thanh công cụ hoặc ... Sau khi tôi đặt:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
    setSupportActionBar(toolbar); 

Nó sẽ mất tất cả người dùng định nghĩa chủ đề và sẽ chỉ nhận được chủ đề cha mẹ .. . Dưới đây là XMLs của tôi (có tôi có rất nhiều các giá trị dự phòng/thử nghiệm như tôi muốn thử tất cả mọi thứ)

<android.support.v7.widget.Toolbar 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/my_awesome_toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     background="?attr/colorPrimary" 
     app:popupTheme="@style/MyActionBarStyle" 
     android:background="?attr/colorPrimary" 
     app:theme="@style/MyActionBarStyle" 
     /> 

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- the theme applied to the application or activity --> 
    <color name="background_test">#410000</color> 
    <color name="font_general">#ffffff</color> 
    <!-- general styles for the action bar --> 
    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> 
     <item name="actionBarStyle">@style/MyActionBarStyle</item> 
     <item name="android:panelColorBackground">@color/background_test</item> 
     <item name="android:headerBackground">@color/background_test</item> 
     <item name="android:colorBackground">@color/background_test</item> 
     <item name="android:backgroundSplit">@color/background_test</item> 

     <item name="backgroundSplit">@color/background_test</item> 
     <item name="colorAccent">@color/background_test</item> 

     <!-- Here we setting appcompat’s actionBarStyle --> 


     <!-- ...and here we setting appcompat’s color theming attrs --> 
     <item name="colorPrimary">@color/background_test</item> 
     <item name="colorPrimaryDark">@color/background_test</item> 
    </style> 

    <style name="MyActionBarStyle"> 
     <item name="colorAccent">@color/background_test</item> 

     <!-- Here we setting appcompat’s actionBarStyle --> 
     <item name="android:panelColorBackground">@color/background_test</item> 
     <item name="android:headerBackground">@color/background_test</item> 
     <item name="android:colorBackground">@color/background_test</item> 
     <item name="android:backgroundSplit">@color/background_test</item> 

     <item name="backgroundSplit">@color/background_test</item> 

     <!-- ...and here we setting appcompat’s color theming attrs --> 
     <item name="colorPrimary">@color/background_test</item> 
     <item name="colorPrimaryDark">@color/background_test</item> 
     <item name="android:background">@color/background_test</item> 

     <item name="background">@color/background_test</item> 
    </style> 

    <style name="AudioFileInfoOverlayText"> 
     <item name="android:paddingLeft">2dp</item> 
     <item name="android:paddingBottom">2dp</item> 
     <item name="android:textColor">#ffffffff</item> 
     <item name="android:textSize">18sp</item> 
     <item name="android:shadowColor">#000000</item> 
     <item name="android:shadowDx">1</item> 
     <item name="android:shadowDy">1</item> 
     <item name="android:shadowRadius">1</item> 
    </style> 



</resources> 

Ảnh chụp màn hình:

http://i.imgur.com/4iohVwP.png 

EDIT mới: Nó sẽ không làm việc từ mã hoặc ...

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
    setSupportActionBar(toolbar); 
    ActionBar test=getSupportActionBar(); 

    test.setLogo(R.drawable.ic_launcher); //this is the only one working... 
    test.setBackgroundDrawable(new ColorDrawable(0x410000)); 

    test.setDisplayShowTitleEnabled(false); 
    test.setDisplayShowTitleEnabled(true); 

EDIT:

Có một số lỗi đã thông báo cho cái mới Appcompat-v7, đây có lẽ là một lỗi:

http://code.google.com/p/android/issues/detail?id=77763&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars 

Trả lời

1

Đây là một ví dụ:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light"> 
    <!-- colorPrimary is used for the default action bar background --> 
    <item name="colorPrimary">@color/my_color</item> 

    <!-- colorPrimaryDark is used for the status bar --> 
    <item name="colorPrimaryDark">@color/my_darker_color</item> 

    <!-- colorAccent is used as the default value for colorControlActivated 
     which is used to tint widgets --> 
    <item name="colorAccent">@color/accent</item> 

    <!-- appcompat’s actionBarStyle --> 
    <item name="actionBarStyle">@style/MyActionBarStyle</item> 

</style> 

Ngoài ra tôi đề nghị bạn sử dụng Toolbar mới.

Bạn có thể tìm thêm thông tin ở đây:

https://chris.banes.me/2014/10/17/appcompat-v21/

+0

Sẽ không bao giờ hoạt động với thanh tác vụ mặc định, màu vẫn giống nhau nhưng đề xuất của Thanh công cụ là tốt nhất vì vậy tôi sẽ không lãng phí thời gian của mình với ActionBar thường xuyên – Arnold

+0

Nó sẽ không hoạt động ... Khi tôi thiết lập thanh công cụ như supportactionbar sẽ mất màu!. Tôi đã chỉnh sửa câu hỏi. – Arnold

+1

Nếu tôi thiết lập "nền" về chủ đề nó sẽ thay đổi màu sắc, nhưng, nó cũng sẽ buộc tất cả mọi thứ khác với cùng một màu sắc vì vậy ngay cả những hình ảnh trong suốt sẽ trông xấu – Arnold

7

Nếu bạn đang sử dụng Toolbar sau đó actionBarStyle của bạn là không sử dụng.

Bạn cần phải phong cách Toolbar trực tiếp:

<android.support.v7.widget.Toolbar 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:minHeight="..." 
    android:background="..." 
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    app:titleTextAppearance="..." 
    app:subtitleTextAppearance="..." /> 

Có nhiều thuộc tính bạn có thể tùy chỉnh, đây là những chỉ là ví dụ.

Tôi không chắc tại sao getSupportActionBar().setBackgroundDrawable(...) không hoạt động cho bạn, tôi vừa thử ở nhiều nơi và hoạt động tốt.

+0

setBackgroundDrawable hiện đang hoạt động. Nhưng, tôi có thực sự cần thiết lập mọi thanh công cụ thay vì sử dụng các kiểu không? Bằng cách nào đó thanh công cụ không nhận được bất kỳ attr từ phong cách và tôi thậm chí đã cố gắng sử dụng các mô tả cha mẹ = "@ style/ThemeOverlay.AppCompat.ActionBar" – Arnold

+0

chủ đề chính là cha mẹ = "Theme.AppCompat.Light.NoActionBar", không chủ đề chính là NoActionBar và chủ đề Thanh công cụ với một thanh công cụ phụ huynh có thể gây ra xung đột? làm thế nào để tôi tuyên bố một phong cách cho tất cả các thanh công cụ của tôi sau đó nếu họ đang hành động như thanh hành động? – Arnold

0

Một giải pháp để có được những AppCompatActivity sử dụng màu chủ đề chính trên Action Bar cho tất cả các cấp api là để khai báo các màu sắc trong cùng một chủ đề như thế này:

<style name="Theme.Custom" parent="Theme.AppCompat"> 

     <!--API < 21 --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 

     <item name="colorButtonNormal">@color/colorButtonNormal</item> 

     <!-- 21 < API this is probably not needed, included for illustration --> 
     <item name="android:colorPrimary" tools:ignore="NewApi">@color/colorPrimary</item> 
     <item name="android:colorPrimaryDark" tools:ignore="NewApi">@color/colorPrimaryDark</item> 
     <item name="android:colorAccent" tools:ignore="NewApi">@color/colorAccent</item> 

     <item name="android:colorButtonNormal" tools:ignore="NewApi">@color/colorButtonNormal</item> 

</style> 

lưu ý: nhớ xóa chủ đề được khai báo trong giá trị-v21 để làm việc này

Các vấn đề liên quan