【问题标题】:Add Up-Navigation Properties in Custom ActionBar Button in Android在 Android 的自定义 ActionBar 按钮中添加向上导航属性
【发布时间】:2015-12-11 16:54:53
【问题描述】:

我想创建自己的ActionBar 布局。

像这样(例如在 Paint 中创建)

是否可以为第二个按钮提供向上导航属性?所以如果我按下它,它会完成这个 Activity 并启动它的父级。

我想要Navigation Drawer 的汉堡图标、向上导航的向上图标和活动标题。

有可能吗?还是已经有解决方案了?

【问题讨论】:

    标签: android


    【解决方案1】:

    实际上,这很容易(虽然它有点笨拙)。 首先,为后退按钮创建一个drawable(最好 - 作为选择器,以区分按下/正常状态:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/back_button_pressed"/>
        <item android:drawable="@drawable/back_button"/>
    </selector>
    

    接下来,将此drawable设置为工具栏的标志toolbar.setLogo(R.drawable.back_button_selector);

    那么剩下的就是设置click-listener了。

    View logoView = getToolbarLogoIcon(toolbar);
    logoView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    
    ...
    
    private View getToolbarLogoIcon(Toolbar toolbar){
        //check if contentDescription previously was set
        boolean hadContentDescription = android.text.TextUtils.isEmpty(toolbar.getLogoDescription());
        String contentDescription = String.valueOf(!hadContentDescription ? toolbar.getLogoDescription() : "logoContentDescription");
        toolbar.setLogoDescription(contentDescription);
        ArrayList<View> potentialViews = new ArrayList<>();
    
        //find the view based on it's content description, set programatically or with android:contentDescription
        toolbar.findViewsWithText(potentialViews,contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
    
        //Nav icon is always instantiated at this point because calling setLogoDescription ensures its existence
        View logoIcon = null;
        if (potentialViews.size() > 0) {
            logoIcon = potentialViews.get(0);
        }
    
        //Clear content description if not previously present
        if (hadContentDescription) {
            toolbar.setLogoDescription(null);
        }
    
        return logoIcon;
    }
    

    (感谢 Nicola 的帖子 here)。或者,如果您不害怕反思,也可以像这样轻松完成:

        try {
            Field declaredField = toolbar.getClass().getDeclaredField("mLogoView");
            declaredField.setAccessible(true);
            View logoView = (View) declaredField.get(toolbar);
            logoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        } catch (Exception ex) {
            //error
        }
    

    另一种可能的解决方案是为 ActionBar 设置自定义布局。 不过,我提倡遵循 UI/UX 指南并仔细检查抽屉式导航是否在辅助活动中必不可少。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      相关资源
      最近更新 更多