获取对溢出图标视图的引用



我想在菜单中的溢出图标上显示一个展示。

这是我的代码:

ShowcaseConfig config = new ShowcaseConfig();
config.setDelay(500); // half second between each showcase view
MaterialShowcaseSequence sequence = new MaterialShowcaseSequence(this, "5");
sequence.setConfig(config);
sequence.addSequenceItem(((ViewGroup) tabLayout.getChildAt(0)).getChildAt(0),
       "Sync your data by turn on the switch", "GOT IT");

我需要对溢出图标View的引用才能添加下一个展示序列:

sequence.addSequenceItem(?, "Click here to display menu", "GOT IT");

如何获取对该View的引用?

您可以通过样式将 id 声明为溢出图标,并使用该 id 查找视图

在 ids.xml 中,声明 id

   <!--Action bar more options -->
    <item name="action_bar_more_options" type="id"/>

在样式上.xml

 <style name="Custom_ActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
        <item name="android:id">@id/action_bar_more_options</item>
    </style>

<style name="Widget_style">
<item name="android:actionOverflowButtonStyle">@style/DarkMail_ActionButtonOverflow</item>
</style>
<item name="android:actionBarWidgetTheme">@style/Widget_style</item>

然后使用 findViewbyId 访问视图:)希望这对你有帮助

获取溢出图标的View有点棘手。

这个答案将指出一种方法。综上所述,应进行以下更改。

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="actionOverflowButtonStyle">@style/MyActionOverflowButtonStyle</item>
</style>
<style name="MyActionOverflowButtonStyle" parent="Widget.AppCompat.ActionButton.Overflow">
    <item name="android:contentDescription">@string/my_custom_content_description</item>
</style>

strings.xml

<string name="my_custom_content_description">some description</string>

在活动的onCreate()

// The content description used to locate the overflow button
final String overflowDesc = getString(R.string.my_custom_content_description);
// The top-level window
final ViewGroup decor = (ViewGroup) getWindow().getDecorView();
// Wait until decor view is laid out
decor.post(new Runnable() {
    @Override
    public void run() {
        // The List that contains the matching views
        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()) {
            return;
        }
        // Do something with the view
        final ImageView overflowIcon = (ImageView) outViews.get(0);
        sequence.addSequenceItem(overflowIcon, "Click here to display menu", "GOT IT");
    }
});

最新更新