我想以编程方式更改溢出图标的颜色,溢出菜单文本和过低菜单的背景颜色。 Google中的解决方案通常使用xml文件。但我想以编程方式进行,因为用户可以更改这些颜色。我也发现了这些答案1,2,但它们对我不起作用。 outViews
数组始终为空。
我该如何更改这些颜色?
我试过这些:
menu.add("overflow");
final String overflowDescription = getActivity().getString(R.string.accessibility_overflow);
final ViewGroup decorView = (ViewGroup) getActivity().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())
{
Log.d(TAG, "empty array");
return;
}
ImageView overflow = (ImageView) outViews.get(0);
overflow.setColorFilter(Color.CYAN);
removeOnGlobalLayoutListener(decorView, this);
}
});
menu.add("overflow");
// The content description used to locate the overflow button
final String overflowDesc = getString(R.string.accessibility_overflow);
// The top-level window
final ViewGroup decor = (ViewGroup) getActivity().getWindow().getDecorView();
final ArrayList<View> outViews = new ArrayList<>();
// Traverse the view-hierarchy and locate the overflow button
decor.findViewsWithText(outViews, overflowDesc,
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
// Guard against any errors
if (outViews.isEmpty()) {
Log.d(TAG,"empty array");
return;
}
// Do something with the view
final ImageButton overflow = (ImageButton) outViews.get(0);
overflow.setImageResource(R.drawable.sil);
string.xml
<string name="accessibility_overflow">overflow</string>
style.xml
<style name="Widget.ActionButton.Overflow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:contentDescription">@string/accessibility_overflow</item>
</style>
<style name="Your.Theme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item>
</style>