在工具栏右侧添加自定义视图

Shu*_*ham 20 android android-actionbar android-toolbar

我正在努力实现这个目标(toolbar红色背景中的计时器):

工具栏中的计时器以及红色背景

我尝试添加customViewtoolbar.它始终是在后箭头旁边的极左侧.与下图中的文字一样,YPTextview添加到工具栏的自定义.在此输入图像描述.

代码toolbar:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/base_activity_toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Run Code Online (Sandbox Code Playgroud)

添加布局的代码:

    LayoutInflater mInflater= LayoutInflater.from(context);
    View mCustomView = mInflater.inflate(R.layout.textview, null);
    toolbar.addView(mCustomView);
Run Code Online (Sandbox Code Playgroud)

这是我正在添加的测试布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:gravity="end"
              android:layout_height="wrap_content">
    <TextView android:layout_width="wrap_content"
              android:text="yp"
              android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我主要缺少一些东西,想不通.拉出我的头发.

cha*_*lag 23

我只是将layout_gravity设置为right并且它有效

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="?android:actionBarSize"
    app:title="NEW REQUESTS"
  >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="blahblah"
      android:layout_gravity="right"  
      />
  </android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

  • @Anarchofascist,是的,你是对的,它没有在工具栏中给出layout_gravity选项,但是如果你自己把它放在那里,它就会被接受. (2认同)

Ano*_*p M 19

你可以在ViewGroups里面添加Toolbar

          <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <RelativeLayout
             ....
            </RelativeLayout>

           </android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

  • 但是,当我执行此类操作时,我会看到已添加的 TextView,但它不再显示工具栏标题。知道为什么吗? (2认同)

Ali*_*azi 8

既然答案有效,但如果你想设置工具栏的标题(很可能你会),那么添加一个RelativeLayout将标题推出右边的视图.在这种情况下,您需要通过设置getSupportActionBar().setDisplayShowTitleEnabled(false)并在要添加到工具栏的其他视图旁边添加自定义TextView 来禁用工具栏标题.举个例子,你可以这样添加ImageView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/base_activity_toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/custom_toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            style="@style/ActionBarTitle"/>

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/custom_toolbar_title"
            android:src="@drawable/arrow"/>

    </RelativeLayout>

</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)


小智 8

刚刚遇到同样的问题,但我找到了一个更简单的(以我的拙见)解决方案.

您可以稍微更改以下代码:

LayoutInflater mInflater= LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.textview, null);
toolbar.addView(mCustomView);
Run Code Online (Sandbox Code Playgroud)

至 :

LayoutInflater mInflater= LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.textview, null);
toolbar.addView(mCustomView, new Toolbar.LayoutParams(Gravity.RIGHT));
Run Code Online (Sandbox Code Playgroud)

然后添加视图,重力设置在右侧!