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

时间:2015-11-25 06:13:58

标签: android android-actionbar android-toolbar

我正在尝试实现此目标(toolbar中带有红色背景的计时器):

A timer in the toolbar along with red background

我想在customView中添加toolbar。它始终是在后箭头旁边的极左侧。与下面图片中的文字YP一样,工具栏中添加了自定义Textviewenter image description here

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" />

添加布局的代码:

    LayoutInflater mInflater= LayoutInflater.from(context);
    View mCustomView = mInflater.inflate(R.layout.textview, null);
    toolbar.addView(mCustomView);

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

<?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>

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

6 个答案:

答案 0 :(得分:19)

您可以在self.tableView.rowHeight = UITableViewAutomaticDimension; [self.tableView setEstimatedRowHeight:85.0];

中添加ViewGroups
Toolbar

答案 1 :(得分:16)

我只是将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>

答案 2 :(得分: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>

答案 3 :(得分:7)

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

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

LayoutInflater mInflater= LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.textview, null);
toolbar.addView(mCustomView);

致:

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

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

答案 4 :(得分:0)

试一试。将R.layout.textview更改为

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TV"
                    android:id="@+id/textView2"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>

答案 5 :(得分:0)

我只是去

<androidx.appcompat.widget.Toolbar
 ...>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="text aligned right" />

</androidx.appcompat.widget.Toolbar>
相关问题