9

Tôi đang sử dụng Action Bar Compat để thanh tác vụ của tôi với ngăn điều hướng tương thích ngược xuống cấp API 9 và tôi muốn thay đổi nền của thanh hành động.Tùy chỉnh (gradient) nền của ActionBar Compat

tôi sao chép mã từ Android Developers:

<?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> 

Và ở đây có vấn đề.

Khi tôi đặt hình ảnh có thể vẽ hoặc màu làm nền, nó hoạt động tốt. Tuy nhiên tôi muốn xác định các nền là một hình gradient, nên actionbar_background của tôi trông giống như:

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line"> 
<gradient 
     android:startColor="@color/ac_bg_start" 
     android:endColor="@color/ac_bg_end" 
     android:type="linear"/> 
<size 
     android:width="1dp" 
     android:height="48dp"/> 
</shape> 

Tôi muốn nó được lặp đi lặp lại theo cách ngang nhưng ngay cả điều này dẫn đến lỗi, trên thực tế, lỗi rất thú vị. Thiết bị thử nghiệm và thậm chí cả trình mô phỏng được khởi động lại khi tôi cố gắng chạy ứng dụng. Tôi đã có thể bắt được DeadObjectException trước khi khởi động lại.

Nền có thể vẽ như thế nào?

Trả lời

16

Tôi hiện đang làm việc trên cùng một nhiệm vụ.

Đây là tệp action_bar_bg.xml trong đó tôi xác định độ dốc cho thanh tác vụ của mình.

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:angle="90" 
     android:centerColor="@color/turquoise_action_bar" 
     android:endColor="@color/dark_turquoise" 
     android:startColor="@color/dark_turquoise" /> 
</shape> 

DeadObjectException

android:shape="line" không thể được sử dụng nếu có một gradient bên trong. Tôi đã thử nghiệm nó; Samsung Galaxy Note 10.1 N8000 của tôi khởi động lại, và có một DeadObjectException.

Loại mẫu gradient linear là giá trị mặc định. Vì vậy, bạn không phải khai báo nó một cách rõ ràng.

Đây là styles.xml của tôi trong các giá trị giá trị thư mục.

<resources> 

    <!-- Base application theme. --> 
    <style name="AppThemeBase" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="android:actionBarStyle">@style/PeopleCanAct</item> 
     <!-- Support library compatibility --> 
     <item name="actionBarStyle">@style/MyActionBar</item> 
    </style> 

    <style name="AppTheme" parent="AppThemeBase"> 
     <!-- All the customizations that are NOT specific to a particular API level can go here --> 
    </style> 

    <!-- ActionBar styles --> 
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
     <item name="android:background">@drawable/action_bar_bg</item> 
     <!-- Support library compatibility --> 
     <item name="background">@drawable/action_bar_bg</item> 
    </style> 

</resources> 

+1

@ style/PeopleCanAct nên được thay đổi để @ style/MyActionBar bạn sẽ nhận được một lỗi biên dịch như-là –

+0

này hoạt động, mặc dù tôi không hiểu phần DeadObjectException, khi nào một thiết bị sẽ nhận được ngoại lệ đó? – Bruce

16

Một cách tiếp cận, mà không sửa đổi styles.xml:

Dưới đây là ví dụ của chúng GradientDrawable trong res/drawable/ab_gradient.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
    android:useLevel="false" > 

    <gradient 
     android:angle="90" 
     android:startColor="#000000" 
     android:endColor="#FFFFFF" 
     android:type="linear" /> 

</shape> 

Bạn có thể thiết lập nó để thanh hành động trong hoạt động của bạn onCreate():

ActionBar actionBar = getActionBar();  
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape)); 

Nếu bạn sử dụng hỗ trợ thư viện v7 (trường hợp của bạn):

// make sure to import android.support.v7.app.ActionBar 
ActionBar actionBar = getSupportActionBar();   
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape)); 

Hoặc nếu bạn sử dụng ActionBarSherlock:

// make sure to import com.actionbarsherlock.app.ActionBar 
ActionBar actionBar = getSherlock().getActionBar();  
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape)); 
+0

Điều này dường như cũng hoạt động, nhưng tôi chấp nhận câu trả lời khác bởi vì nó giải thích những gì đã sai mã của tôi. –

+0

@ Max77 .getDrawable hiện không còn được dùng nữa. Mã mới để thay thế phương thức không dùng nữa là gì? – iOSAndroidWindowsMobileAppsDev

+1

@JqueryNinja kiểm tra câu trả lời hay này: http://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22 – Max77

0

tôi cũng sử dụng mẫu mã từ Android Developer và sử dụng XML dốc giống như của bạn.

<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="line"> 

Tôi thấy tệp khác giữa tệp này và của tôi là android:shape="line"/android:shape="rectangle".

Vì vậy, tôi cố gắng thay đổi hình chữ nhật của mình thành dòng. Ứng dụng của tôi cũng xảy ra cùng một ngoại lệ và hệ điều hành được khởi động lại. Có lẽ hình dạng là điểm mấu chốt.

+0

Điều này không thực sự trả lời câu hỏi. Nếu bạn có một câu hỏi khác, bạn có thể hỏi nó bằng cách nhấp vào [Hỏi câu hỏi] (http://stackoverflow.com/questions/ask). Bạn cũng có thể [thêm tiền thưởng] (http://stackoverflow.com/help/privileges/set-bounties) để thu hút thêm sự chú ý đến câu hỏi này khi bạn có đủ [danh tiếng] (http://stackoverflow.com/help/ whats-danh tiếng). – maszter