Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

android - How to get the "up" button used on the Toolbar?

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);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...