一:静态替换
通过在主题中添加
<item name="android:actionOverflowButtonStyle">@style/OverFlow</item>
后重写actionbar中 OverFlow的属性
代码如下
<style name="MmsTheme" parent="@android:style/Theme.Material.Light"> <item name="android:colorPrimary">@color/primary</item> <item name="android:colorPrimaryDark">@color/primary_dark</item> <item name="android:textColorPrimary">@color/white</item> <item name="android:textColor">@*android:color/primary_text_light</item> <item name="android:colorAccent">@color/primary</item> <item name="android:actionBarSize">@dimen/atcionbar_size</item> <item name="android:actionOverflowButtonStyle">@style/MyOverflowButton</item> </style >重写OverFlowButton<!-- MyOverflowButton --> <style name="MyOverflowButton" parent="@android:style/Widget.Material.ActionButton.Overflow"> <item name="android:contentDescription">@string/overflow</item> <item name="android:src">@drawable/ic_menu_overflow</item> </style>二:代码中动态替换
先依照静态添加方法配置并在重写的OverFlowButton的样式中添加android:contentDescription属性(上面蓝色的代码);
代码中动态替换的代码如下:
final String overflowDesc = getString(R.string.overflow);//找到用于定位溢出按钮的描述 final ViewGroup decor = (ViewGroup) getWindow().getDecorView();//获取整个界面 //延迟一段时间确保OverFlowButton已经被加载进来 decor.postDelayed(new Runnable() { @Override public void run() { final ArrayList<View> outViews = new ArrayList<View>(); //匹配所有的视图组件得到OverFlowButton decor.findViewsWithText(outViews, overflowDesc,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION); if (outViews.isEmpty()) { return; } //强转修改图片 final ImageButton overflow = (ImageButton) outViews.get(0); overflow.setImageResource(R.drawable.ic_menu_overflow_black); } }, 500);三:修改menu布局文件通过menu与item的相互嵌套实现修改OverFlowButton的效果
布局文件
并且这时可以在代码中通过“menu.findItem(int id).setIcon(R.drawable.xxx)”进行图标的替换<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:orderInCategory="100" android:showAsAction="always" android:title="@string/action_search"/> <item android:id="@+id/overflow_menus" android:actionProviderClass="@android:style/Widget.Holo.ActionButton.Overflow" android:orderInCategory="100" android:showAsAction="always" android:title="@string/accessibility_overflow"> <menu> <item android:id="@+id/action__delete_all" android:showAsAction="never" android:title="@string/menu_delete_all"/> <item android:id="@+id/action_setting" android:orderInCategory="100" android:showAsAction="never" android:title="@string/menu_setting"/> </menu> </item> </menu>
四:代码中动态修改actionbar的背景颜色
在style文件中添加
<drawable name="BackBar">#fff</drawable>
然后在onCreate()最后调用setbackgroundDrawable()方法getActionBar().setBackgroundDrawable(this.getBaseContext(). getResources().getDrawable(R.drawable.BackBar)); getActionBar().show();
OverFlowButton的替换方式
最新推荐文章于 2022-08-24 16:55:29 发布