如何在工具栏中使用图像作为副标题?

How to use image as subtitle in toolbar?

我将工具栏用作操作栏。我可以设置字幕,但是是否可以设置字幕? ImageView 作为副标题而不是纯文本?

工具栏的行为与任何其他视图一样。因此,您可以进入工具栏的 xml 定义,将其垂直拆分(使用权重和)以容纳您的 ImageView 容器,并在其中放置一个 ImageView。 然后,在您的 Activity 中,您可以设置图像:

ImageView subtitleImage = mToolbar.findViewById(R.id.subtitle_imageview);
subtitleImage.setImageResource(R.drawable.your_image_view);

Toolbar只是一个ViewGroup,所以你可以在里面定义你喜欢的视图。

例如:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize">

        <LinearLayout  
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical">

              <TextView android:id="@+id/textView"..../>

              <ImageView 
                  android:id="@+id/imageView"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content" />

        </LinearLayout>


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

然后在你的代码中:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView title = mToolbar.findViewById(R.id.textView);
ImageView image = mToolbar.findViewById(R.id.imageView);