如何获得"向上QUOT;工具栏上的按钮使用? [英] How to get the "up" button used on the Toolbar?

查看:318
本文介绍了如何获得"向上QUOT;工具栏上的按钮使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简短的问题:

我试图强制执行操作杆(使用工具栏)使用LTR对齐。我已经成功做了布局本身使用LTR,而不是向上按钮(像我那样 此处 ,之前工具栏 介绍)。

看来这种观点没有一个ID,我想用getChildAt()是太冒险了。

谁能帮助?


答案

下面是我发现要解决这个问题,基于 这个答案 的一种方式。

我说得那么它guarranteed发现只有向上按钮,不管它,它会恢复到previous状态以前。

这里的code:

  @TargetApi(Build.VERSION_ codeS.JELLY_BEAN_MR1)
  @覆盖
  公共布尔onCreateOptionsMenu(最后菜单菜单)
    {
    //< =做动作栏菜单preparetions正常的东西
    if(VERSION.SDK_INT>=VERSION_$c$cS.JELLY_BEAN_MR1&&getResources().getConfiguration().getLayoutDirection()==View.LAYOUT_DIRECTION_RTL)
      {
      最后的ArrayList<视图> outViews =新的ArrayList<>();
      最后的CharSequence previousDesc = _toolbar.getNavigationContentDescription();
      对于(INT ID = 0 ;; ++ ID)
        {
        最后弦乐uniqueContentDescription = Integer.toString(ID);
        _toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        如果(!outViews.isEmpty())
          继续;
        _toolbar.setNavigationContentDescription(uniqueContentDescription);
        _toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        如果(outViews.isEmpty())
           如果(BuildConfig.DEBUG)
                    抛出新的RuntimeException(
                            你应该调用该函数只有在工具栏上已经有意见);
                其他
                    打破;
        outViews.get(0).setRotation(180°F);
        打破;
        }
      _toolbar.setNavigationContentDescription(previousDesc);
      }
    //
    返回super.onCreateOptionsMenu(菜单);
    }
 

解决方案
  

看来这种观点并没有一个ID

您说得对,<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/Toolbar.java#L985"相对=nofollow>导航视图编程方式创建,从来没有设置一个ID。但你仍然可以找到它通过使用<一个href="https://developer.android.com/reference/android/view/View.html#findViewsWithText(java.util.ArrayList%3Candroid.view.View%3E,%20java.lang.CharSequence,%20int)"相对=nofollow> View.findViewsWithText

View.findViewsWithText 有两个标志:

  • View.FIND_VIEWS_WITH_TEXT

  • <一个href="https://developer.android.com/reference/android/view/View.html#FIND_VIEWS_WITH_CONTENT_DESCRIPTION"相对=nofollow> View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION

导航视图的默认内容描述是向上导航或资源ID为 abc_action_bar_up_description 的AppCompat和 action_bar_up_description 为框架的,但你可以自己使用<轻松应用href="https://developer.android.com/reference/android/widget/Toolbar.html#setNavigationContentDescription(java.lang.CharSequence)"相对=nofollow> Toolbar.setNavigationContentDescription

下面是一个例子实现:

 最后工具条工具栏= ...;
toolbar.setNavigationContentDescription(上);
setActionBar(工具栏);

最后的ArrayList&LT;视图&gt; outViews = Lists.newArrayList();
toolbar.findViewsWithText(outViews,上,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);

outViews.get(0).setRotation(180°F);
 

结果

This is a short question:

I'm trying to force the action bar (used by a Toolbar) to use LTR alignment. I've succeeded making the layout itself use LTR, but not the "up" button (as I've done here, before Toolbar was introduced) .

It seems this view doesn't have an ID, and I think using getChildAt() is too risky.

Can anyone help?


The answer

Here's one way I've found to solve this, based on this answer .

I made it so that it is guarranteed to find only the "up" button, and whatever it does, it will revert back to the previous state it was before.

Here's the code:

  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  @Override
  public boolean onCreateOptionsMenu(final Menu menu)
    {
    // <= do the normal stuff of action bar menu preparetions
    if(VERSION.SDK_INT>=VERSION_CODES.JELLY_BEAN_MR1&&getResources().getConfiguration().getLayoutDirection()==View.LAYOUT_DIRECTION_RTL)
      {
      final ArrayList<View> outViews=new ArrayList<>();
      final CharSequence previousDesc=_toolbar.getNavigationContentDescription();
      for(int id=0;;++id)
        {
        final String uniqueContentDescription=Integer.toString(id);
        _toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        if(!outViews.isEmpty())
          continue;
        _toolbar.setNavigationContentDescription(uniqueContentDescription);
        _toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        if (outViews.isEmpty())
           if (BuildConfig.DEBUG)
                    throw new RuntimeException(
                            "You should call this function only when the toolbar already has views");
                else
                    break;
        outViews.get(0).setRotation(180f);
        break;
        }
      _toolbar.setNavigationContentDescription(previousDesc);
      }
    //
    return super.onCreateOptionsMenu(menu);
    }

解决方案

It seems this view doesn't have an ID

You're right, the navigation view is created programmatically and never sets an id. But you can still find it by using View.findViewsWithText.

View.findViewsWithText comes with two flags:

The navigation view's default content description is "Navigate up" or the resource id is abc_action_bar_up_description for AppCompat and action_bar_up_description for the framework's, but you can easily apply your own using Toolbar.setNavigationContentDescription.

Here's an example implementation:

final Toolbar toolbar = ...;
toolbar.setNavigationContentDescription("up");
setActionBar(toolbar);

final ArrayList<View> outViews = Lists.newArrayList();
toolbar.findViewsWithText(outViews, "up", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);

outViews.get(0).setRotation(180f);

Results

这篇关于如何获得&QUOT;向上QUOT;工具栏上的按钮使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆