在工具栏右侧添加自定义视图
问题描述:
我正在尝试实现这一目标(toolbar
中的计时器为红色背景):
I am trying to achieve this(A timer in toolbar
with red background):
我正在尝试在toolbar
中添加customView
.它总是以向后箭头旁边的最左端结束.就像下面图像中的文本YP
一样,是添加到工具栏的自定义Textview
..
I am trying to add customView
in toolbar
. It always end's up being on extreme left just beside the back arrow. Like the text YP
in image below is a custom Textview
added to toolbar..
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);
这是我现在为测试添加的布局:
This is the layout I am adding right now for testing:
<?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>
我主要缺少一些东西,无法弄清楚.拔掉我的头发.
I am missing something majorly, could not figure out. Pulling my hairs out.
答
您可以在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>