【发布时间】:2017-08-10 13:35:31
【问题描述】:
今天我遇到了Toolbar 自定义标头中的一个奇怪问题。
我在中心使用TextView,在Toolbar 右侧使用ImageView。所以我在xml中使用了下面的代码行,请检查一次。
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#3EC3D6"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/headerTxt"
style="@style/Base.TextAppearance.AppCompat.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center"
android:layout_gravity="center"
android:maxLines="1"
android:text="Tile"
android:textColor="@android:color/black" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/chat_new_icon"
android:layout_gravity="right"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
我已经使用RelativeLayout 解决了这个问题,但它也不起作用。请检查下面的代码
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#3EC3D6"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/headerTxt"
style="@style/Base.TextAppearance.AppCompat.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:maxLines="1"
android:text="Tile"
android:textColor="@android:color/black" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/chat_new_icon"
android:layout_alignParentRight="true"
android:layout_gravity="right"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
我已经参考了这个解决方案,但它对我不起作用。请检查一次链接。
1. First Link
2. Second Link
3. Third Link
4. Forth Link
我需要TextView 应该在Toolbar 的中心和ImageView 应该在Toolbar 的右侧。请帮我解决这个问题。
【问题讨论】:
-
尝试添加 app:contentInsetEnd="0dp" app:contentInsetEndWithActions="0dp" app:contentInsetLeft="0dp" app:contentInsetRight="0dp" app:contentInsetStart="0dp" app:contentInsetStartWithNavigation=工具栏中的“0dp”
-
@DivyeshPatel 试过你的建议没用 :(
-
从您在问题中发布的第一个布局中删除
Toolbar内的LinearLayout。 -
没问题。 @ 987654327@,我的回答解释了为什么不居中,以及如何准确设置孩子@ 987654343@s,以防我的cmets不太清楚。请注意,那里的
LinearLayout仅用于垂直堆叠这两个TextViews。它不会填满Toolbar,也不会包含所有内容。 -
@MikeM。感谢有价值的评论作品:)
标签: android android-layout layout android-toolbar text-alignment