【问题标题】:Android Toolbar "Up" Arrow in custom implementation自定义实现中的 Android 工具栏“向上”箭头
【发布时间】:2014-12-16 22:06:09
【问题描述】:

我正在制作一个非常自定义的工具栏(不要惊慌,我保证这是好事,不是坏事),我想放置“向上”箭头(当有父 Activity 集时出现的箭头或出于其他原因,并且在自定义位置对 Lollipop 有很好的涟漪触摸效果。

有没有办法将此箭头作为视图或资产在工具栏中的其他位置使用?

我什至找不到箭头的图像资源。有什么想法吗?

例如,我希望 top 能够执行以下操作:

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

【问题讨论】:

    标签: android android-actionbar android-navigation android-toolbar


    【解决方案1】:

    也许this 会有所帮助,看起来像是您想要的东西,或者您想要完全自定义具有不同位置和视图的向上按钮?

    【讨论】:

    • 我希望能够像视图一样完全控制向上箭头。
    【解决方案2】:

    如果您不想在Toolbar 中将箭头图标作为自定义视图放置,或者您可以将内容描述设置为导航按钮并稍后查找视图参考。每个ViewViewGroup 都支持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;
     }
    

    【讨论】:

      【解决方案3】:

      Toolbar 本身就是一个 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);
      

      【讨论】:

      • 我的问题是如何设置类似向上箭头的东西。我知道如何在工具栏中设置视图。
      猜你喜欢
      • 2015-10-14
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 2015-11-22
      相关资源
      最近更新 更多