Shu*_*ham 20 android android-actionbar android-toolbar
我正在努力实现这个目标(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" />
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)
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)
既然答案有效,但如果你想设置工具栏的标题(很可能你会),那么添加一个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
刚刚遇到同样的问题,但我找到了一个更简单的(以我的拙见)解决方案.
您可以稍微更改以下代码:
Run Code Online (Sandbox Code Playgroud)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));
Run Code Online (Sandbox Code Playgroud)
然后添加视图,重力设置在右侧!
| 归档时间: |
|
| 查看次数: |
20552 次 |
| 最近记录: |