我正在尝试实现这个功能(在工具栏中带有红色背景的计时器):
我试图在工具栏中添加customView
,但它总是出现在极左边,就在返回箭头旁边。像下面图像中的文本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>
我正在错过一个重要的东西,无法弄清楚。让我抓狂。
layout_width
更改为match_parent
。 - Mike M.