我想提出一个非常自定义工具栏(不要惊慌,这是好的,不是邪恶的,我保证),我想提出,当有家长活动设置或出现向上箭头(箭头因其他原因,并在自定义的位置上棒棒堂漂亮的波纹触摸效果)。
有没有办法让这个箭头视图或资产使用别的地方在工具栏?
我甚至不能找到箭头的图片资源。任何想法?
举个例子,我想上面可以做一些事情,如:
mToolbar.addView(arrowView,0);
mToolbar.addView(titleview的,1);
解决方案
如果你不想把在工具栏
自定义视图箭头图标,或者您可以设置内容描述的导航按钮,后来发现视图引用。每个查看
或的ViewGroup
支持的寻找具有内容描述的看法。
公共静态视图getNavigationIcon(工具栏工具栏){
//检查是否contentDescription previously设置
布尔hadContentDescription = TextUtils.isEmpty(toolbar.getNavigationContentDescription());
字符串contentDescription =!hadContentDescription? toolbar.getNavigationContentDescription():navigationIcon;
toolbar.setNavigationContentDescription(contentDescription);
ArrayList的<视图> potentialViews =新的ArrayList<视图>();
//查找基于它的视图的内容描述,程序设定或与Android:contentDescription
toolbar.findViewsWithText(potentialViews,contentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
//导航图标总是实例化在这一点上,因为调用setNavigationContentDescription保证了它的存在
查看navIcon = NULL;
如果(potentialViews.size()大于0){
navIcon = potentialViews.get(0); //导航图标的ImageButton
}
//清除内容的描述,如果没有previously present
如果(hadContentDescription)
toolbar.setNavigationContentDescription(空);
返回navIcon;
}
I am making a very custom Toolbar (don't freak, it's for good, not evil, I promise), and I want to put the "Up" arrow (the arrow that appears when there is a parent Activity set or for other reasons and has the nice ripple touch effect on Lollipop) in a custom location.
Is there a way to get this arrow as a View or asset to use somewhere else in the Toolbar?
I could not even find the image asset for the arrow. Any ideas?
For an example, I want top be able to do something like:
mToolbar.addView(arrowView, 0);
mToolbar.addView(titleView, 1);
解决方案
If you don't want to place arrow icon as custom view in Toolbar
, alternatively you can set content description to the navigation button and find view reference later. Every View
or ViewGroup
supports finding views with content description.
public static View getNavigationIcon(Toolbar toolbar){
//check if contentDescription previously was set
boolean hadContentDescription = TextUtils.isEmpty(toolbar.getNavigationContentDescription());
String contentDescription = !hadContentDescription ? toolbar.getNavigationContentDescription() : "navigationIcon";
toolbar.setNavigationContentDescription(contentDescription);
ArrayList<View> potentialViews = new ArrayList<View>();
//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 setNavigationContentDescription ensures its existence
View navIcon = null;
if(potentialViews.size() > 0){
navIcon = potentialViews.get(0); //navigation icon is ImageButton
}
//Clear content description if not previously present
if(hadContentDescription)
toolbar.setNavigationContentDescription(null);
return navIcon;
}
上一篇:显示页面从底部到顶部页面
下一篇:安卓:需要android.support.v7.app.ActionBar发现android.app.ActionBar发现、support、android、ActionBar