这是一个简短的问题:
我试图强制操作栏(由工具栏使用)使用 LTR 对齐方式。我已经成功地使布局本身使用 LTR,但没有使用“向上”按钮(正如我所做的那样)here https://stackoverflow.com/q/22602453/878126, 前Toolbar http://developer.android.com/reference/android/support/v7/widget/Toolbar.html被介绍)。
看来这个视图没有 ID,我认为使用 getChildAt() 风险太大。
有人可以帮忙吗?
答案
这是我发现解决这个问题的一种方法,基于这个答案 https://stackoverflow.com/a/27180584/878126 .
我这样做是为了保证只找到“向上”按钮,无论它做什么,它都会恢复到之前的状态。
这是代码:
@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);
}
该视图似乎没有 ID
你说得对,导航视图以编程方式创建 https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/Toolbar.java#L985并且从不设置 id。但您仍然可以通过使用找到它View.findViewsWithText https://developer.android.com/reference/android/view/View.html#findViewsWithText(java.util.ArrayList%3Candroid.view.View%3E,%20java.lang.CharSequence,%20int).
View.findViewsWithText
有两个标志:
View.FIND_VIEWS_WITH_TEXT https://developer.android.com/reference/android/view/View.html#FIND_VIEWS_WITH_TEXT
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION https://developer.android.com/reference/android/view/View.html#FIND_VIEWS_WITH_CONTENT_DESCRIPTION
导航视图的默认内容描述是“向上导航”或者资源 ID 是abc_action_bar_up_description
对于 AppCompat 和action_bar_up_description
对于框架的,但您可以轻松应用自己的使用Toolbar.setNavigationContentDescription https://developer.android.com/reference/android/widget/Toolbar.html#setNavigationContentDescription(java.lang.CharSequence).
这是一个示例实现:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)