Android的工具栏"多达"箭在定制实现 [英] Android Toolbar "Up" Arrow in custom implementation

查看:138
本文介绍了Android的工具栏"多达"箭在定制实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个非常自定义工具栏(不要惊慌,这是好的,不是邪恶的,我保证),我想提出,当有家长活动设置或出现向上箭头(箭头因其他原因,并在自定义的位置上棒棒堂漂亮的波纹触摸效果)。

有没有办法让这个箭头视图或资产使用别的地方在工具栏?

我甚至不能找到箭头的图片资源。任何想法?

举个例子,我想上面可以做一些事情,如:

  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的工具栏&QUOT;多达&QUOT;箭在定制实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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