Android的工具栏“上”箭头自定义实现(Android Toolbar “Up” Arrow i

2019-10-21 11:06发布

我想提出一个非常自定义工具栏(不要惊慌,这是好的,不是邪恶的,我保证),我希望把出现的“向上”箭头(箭头时,有一组父活动或其他原因并在自定义的位置上棒棒堂很好的涟漪触摸效果)。

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

我甚至不能找到箭头图片素材。 有任何想法吗?

举一个例子,我想上面能够做这样的事情:

mToolbar.addView(arrowView, 0);
mToolbar.addView(titleView, 1);

Answer 1:

也许这将帮助,看起来像的东西,你想要的,或者你想完全自定义的按钮了不同的立场和观点?



Answer 2:

如果你不想把箭头图标,自定义视图中Toolbar ,或者你可以将内容描述设定导航按钮后来发现观看基准。 每次ViewViewGroup支持寻求与内容描述的观点 。

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;
 }


Answer 3:

工具栏本身就是一个ViewGroup是你可以为如添加不同的看法。

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" >

     <TextView ..../>
     <ImageView ...../>
</android.support.v7.widget.Toolbar>

请参阅您的工具栏,并从你的Java代码的元素有控制权为:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
ImageView img = (ImageView) toolbar.findViewById(R.id.imgv);


文章来源: Android Toolbar “Up” Arrow in custom implementation