2014-10-31 41 views
8

Trong dự án của tôi cho đến một vài ngày trước, tôi đã sử dụng thanh hành động sherlock nhưng tôi đã dicided để thay đổi toàn bộ phong cách actionbar thành thiết kế material design mới. Về chủ đề cũ của tôi cho biểu tượng trở lại và tràn Tôi đã có một số biểu tượng tùy chỉnh được giải quyết trên chủ đề của tôi. Nhưng trên appcompat tôi đã cố gắng xác định nó về chủ đề của tôi, nơi tôi sử dụng nó trên một thanh công cụ nhưng nó không hoạt động. Bất cứ ai cũng biết làm thế nào để làm điều này có thể?Android: AppCompat 21, cách thay đổi biểu tượng quay lại và biểu tượng tràn vào biểu tượng tùy chỉnh?

chủ đề của tôi mà tôi sử dụng trên thanh công cụ (tạo với phòng thu tài sản): xml

<style name="Theme.Themewiz" parent="@style/Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:actionBarStyle">@style/ActionBar.Solid.Themewiz</item> 

    <item name="android:actionBarItemBackground">@drawable/selectable_background_themewiz</item> 
    <item name="android:popupMenuStyle">@style/PopupMenu.Themewiz</item> 
    <item name="android:dropDownListViewStyle">@style/DropDownListView.Themewiz</item> 
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Themewiz</item> 
    <item name="android:actionDropDownStyle">@style/DropDownNav.Themewiz</item> 
    <item name="android:actionModeBackground">@drawable/cab_background_top_themewiz</item> 
    <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_themewiz</item> 
    <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Themewiz</item> 

      <!-- Light.DarkActionBar specific --> 
    <item name="android:actionBarWidgetTheme">@style/Theme.Themewiz.Widget</item> 

    <item name="android:actionOverflowButtonStyle">@style/Theme.AppCompat.Overflow</item> 

</style> 

<style parent="@style/Widget.AppCompat.ActionButton.Overflow" name="Theme.AppCompat.Overflow"> 

    <item name="android:src">@drawable/ic_action_overflow</item> 

</style> 

Toolbar:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/my_awesome_toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimaryDark" 
    app:theme="@style/Theme.Themewiz" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 
+0

Mã? XML? Không có nó thì không thể giúp được gì. –

+0

Tôi chỉnh sửa bài đăng gốc của mình bằng xmlfrom chủ đề và xml từ thanh công cụ. – billiout

Trả lời

12

Sử dụng appcompat 21 để đạt được này là khá dễ dàng.

Để thay đổi biểu tượng quay lại, bạn phải sử dụng thanh công cụ hỗ trợ làm thanh tác vụ thay thế. This post by Chris Banes sẽ giúp bạn bắt đầu.

Khi bạn đã thực hiện thêm các thanh công cụ, tất cả các bạn phải làm là:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_id); 
toolbar.setNavigationIcon(R.drawable.back_icon); 

Để thay đổi biểu tượng tràn bộ mặt khác, cách dễ nhất là thêm một phong cách tùy chỉnh để chủ đề chính tờ khai. Một cái gì đó như:

<resources> 
    <!-- Base application theme. --> 
    <style name="Your.Theme" parent="@style/Theme.AppCompat"> 
     <!-- Your theme items go here --> 

     <item name="actionOverflowButtonStyle">@style/OverFlow</item> 
    </style> 

    <!-- Styles --> 
    <style name="OverFlow" parent="@android:style/Widget.AppCompat.ActionButton.Overflow"> 
     <item name="android:src">@drawable/overflow_icon</item> 
    </style> 

</resources> 

Hy vọng điều đó sẽ hữu ích.

+2

Làm việc cho tôi, ngoại trừ sử dụng '@ style/Widget.AppCompat.ActionButton.Overflow' thay vì' @android: style/Widget.AppCompat.ActionButton.Overflow' – Birrel

+0

Nó làm việc cho tôi, nhưng nó sử dụng một phiên bản thực sự lớn của tôi biểu tượng. Tôi có nhiều biểu tượng trong nhiều thư mục có thể vẽ, nhưng dường như không chọn kích thước phù hợp với các biểu tượng khác của tôi. –

+0

Thực ra, tôi chỉ có một giải pháp cho vấn đề mà tôi gặp phải. Tôi đã phải sử dụng biểu tượng 24dp. Bạn có thể thấy câu hỏi SO của tôi và câu trả lời của nó ở đây: http: // stackoverflow.com/questions/32450144/custom-overflow-icon-là-quá-big-sử dụng-android-support-v7-and-appcompat –

4

Tôi đã làm theo cách này và nó làm việc cho tôi

<style name="AppTheme.Base" parent="Theme.AppCompat.Light"> 
    <item name="actionOverflowButtonStyle">@style/OverFlow</item> 
</style> 

<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow"> 
    <item name="android:src">@drawable/custom_button</item> 
</style> 
-3

Đối với séc Thay đổi Navigation mũi tên bên dưới đang

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha); 
upArrow.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP); 
getSupportActionBar().setHomeAsUpIndicator(upArrow); 

Đầu tiên chúng ta cần thêm các thuộc tính mô tả nội dung vào biểu tượng menu tràn trong tệp styles.xml. Nó có thể là bất kỳ chuỗi nào, chỉ cần tìm đúng chế độ xem sau trong mã bằng mô tả của nó.

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.MyTheme"> 
    <!-- Customize your theme here. --> 
    <item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item> 
</style> 

<style name="Widget.ActionButton.Overflow" parent="@android:style/Widget.Holo.ActionButton.Overflow"> 
    <item name="android:contentDescription">@string/accessibility_overflow</item> 
</style> 

Thứ hai, chúng tôi thực hiện các phương pháp chịu trách nhiệm cho việc tìm kiếm và colorizing các Overflow Biểu tượng:

/* It's important to set overflowDescription atribute in styles, so we cangrab the reference 
* to the overflow icon. Check: res/values/styles.xml 
* @param activity 
* @param colorFilter 
*/ 
private static void setOverflowButtonColor(final Activity activity, final PorterDuffColorFilter colorFilter) { 
    final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description); 
    final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); 
    final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver(); 
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      final ArrayList<View> outViews = new ArrayList<View>(); 
      decorView.findViewsWithText(outViews, overflowDescription, 
        View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION); 
      if (outViews.isEmpty()) { 
       return; 
      } 
      TintImageView overflow=(TintImageView) outViews.get(0); 
      overflow.setColorFilter(colorFilter); 
      removeOnGlobalLayoutListener(decorView,this); 
     } 
    }); 
} 

/***Use below code in your activity screen for apply color*/ 

final PorterDuffColorFilter colorFilter= new PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY); 
setOverflowButtonColor(this,colorFilter); 

Vui lòng kiểm tra dưới đây liên kết cho biết hơn https://snow.dog/blog/how-to-dynamicaly-change-android-toolbar-icons-color/

+0

Vui lòng kiểm tra cập nhật mới của tôi cho tôi biết nếu có bất kỳ câu hỏi nào – Piyush

+0

Có thể nhận nút tràn tại thời gian chạy, mà không cần thiết lập phong cách? –

4

Bắt quan điểm bản thân là một chút khó khăn , nhưng việc đặt biểu tượng và/hoặc màu sắc của chúng có thể sử dụng các chức năng của Thanh công cụ. Ví dụ:

toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha); 
final Drawable navigationIcon = toolbar.getNavigationIcon(); 
toolbar.setNavigationIcon(getTintedDrawable(this, navigationIcon, 0xffff0000)); 
final Drawable overflowIcon = toolbar.getOverflowIcon(); 
toolbar.setOverflowIcon(getTintedDrawable(this, overflowIcon, 0xffff0000)); 

public static Drawable getTintedDrawable(@NonNull Context context, @NonNull Drawable inputDrawable, @ColorInt int color) { 
    Drawable wrapDrawable = DrawableCompat.wrap(inputDrawable); 
    DrawableCompat.setTint(wrapDrawable, color); 
    DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN); 
    return wrapDrawable; 
} 
+0

thanks guys trick của bạn giúp tôi ... –

+0

Làm việc tốt xung quanh! Điều này làm việc cho tôi – Arjun

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