Добавить пользовательский вид справа от панели инструментов
Я пытаюсь достичь этого (таймер в toolbar
с красным фоном):
![Таймер на панели инструментов вместе с красным фоном]()
Я пытаюсь добавить customView
в toolbar
. Он всегда находится на крайнем левом краю рядом со спиной. Как и текст YP
на изображении ниже, пользовательский Textview
добавлен в панель инструментов.
.
Код 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>
Мне что-то не хватает, не могу понять. Вытащить волосы.
Ответы
Ответ 1
Вы можете добавить 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>
Ответ 2
Данные ответы работают, но если вы хотите установить заголовок на панели инструментов (скорее всего, вы это сделаете), то добавление 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
Я просто установил 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>
Ответ 4
У меня была такая же проблема, но я нашел более простое (по моему скромному мнению) решение.
Вы можете немного изменить свой код:
LayoutInflater mInflater= LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.textview, null);
toolbar.addView(mCustomView);
To:
LayoutInflater mInflater= LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.textview, null);
toolbar.addView(mCustomView, new Toolbar.LayoutParams(Gravity.RIGHT));
Затем изображение добавляется с гравитацией вправо!
Ответ 5
попробуйте. измените 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>