and*_*per 5 android right-to-left android-actionbar android-toolbar
这是一个简短的问题:
我试图强制操作栏(由工具栏使用)使用LTR对齐.我成功地使布局本身使用LTR,而不是"向上"按钮(就像我在这里所做的那样,在引入工具栏之前).
看来这个视图没有ID,我认为使用getChildAt()风险太大.
有人可以帮忙吗?
基于这个答案,这是我发现解决这个问题的一种方法.
我这么做是为了保证只找到"向上"按钮,无论它做什么,它都会恢复到以前的状态.
这是代码:
@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);
}
Run Code Online (Sandbox Code Playgroud)
看来这个视图没有ID
你是对的,导航视图是以编程方式创建的,从不设置id.但你仍然可以通过使用找到它View.findViewsWithText.
View.findViewsWithText 附带两个标志:
导航视图的默认内容描述是"向上导航"或资源ID abc_action_bar_up_description用于AppCompat和action_bar_up_description框架,但您可以轻松应用自己的使用Toolbar.setNavigationContentDescription.
这是一个示例实现:
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);
Run Code Online (Sandbox Code Playgroud)
结果

| 归档时间: |
|
| 查看次数: |
1791 次 |
| 最近记录: |