[英]Custom Toolbar side textView which is in the end of Toolbar title got detached when back button or hamberger Button is enable
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/toolbar_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="?android:attr/actionBarSize"
android:fontFamily="@font/inter_semi_bold"
android:textColor="@color/layout_main_text_color"
android:textSize="@dimen/_20ssp"
tools:text="Toolbar" />
<TextView
android:id="@+id/total_job_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/toolbar_text_view"
android:background="@drawable/rectangle_blue_gradient_with_thirty_radius"
android:paddingLeft="@dimen/_7sdp"
android:paddingTop="@dimen/_1sdp"
android:paddingRight="@dimen/_7sdp"
android:paddingBottom="@dimen/_1sdp"
android:text="1425"
android:textColor="@color/white"
android:textSize="@dimen/_7ssp" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
在这个项目中,我使用导航组件。 我想在“工作委员会”旁边的蓝色边框中显示数字。 我看了一下这个解决方案。 启用后退按钮时工具栏标题不在中心。该解决方案能够解决工具栏文本居中而不是数字居中的问题。 我想要这种类型的 output
最后我解决了这个问题。 罪魁祸首是 contentinsetstart,它在 Toolbar 的开头使用 72 dp 作为填充,大小为 72 dp。 所以我做了什么,我只是在toolbarTextView中给出了一个负的72边距。 一切都按预期运行:)
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/toolbar_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-72dp"
android:fontFamily="@font/inter_semi_bold"
android:textColor="@color/layout_main_text_color"
android:textSize="@dimen/_20ssp"
tools:text="Toolbar" />
<TextView
android:id="@+id/total_job_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/toolbar_text_view"
android:background="@drawable/rectangle_blue_gradient_with_thirty_radius"
android:paddingLeft="@dimen/_7sdp"
android:paddingTop="@dimen/_1sdp"
android:paddingRight="@dimen/_7sdp"
android:paddingBottom="@dimen/_1sdp"
android:text="1425"
android:textColor="@color/white"
android:textSize="@dimen/_7ssp"
android:visibility="invisible" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
和destinationChangeListener 看起来完全一样
navController.addOnDestinationChangedListener { _, destination, _ ->
if (destination.id != R.id.tutorHomeFragment) {
total_job_text_view.visibilityGone()
}
with(toolbar_text_view) {
when(destination.id) {
R.id.tutorHomeFragment -> text = getString(R.string.job_board)
R.id.tutorProfileFragment -> text = getString(R.string.profile)
R.id.tutorStatusFragment -> text = getString(R.string.status)
R.id.tutorSettingFragment -> text = getString(R.string.settings)
R.id.tutorMessageFragment -> text = getString(R.string.message)
R.id.confirmationLetterFragment -> text = getString(R.string.confirmation_letter)
R.id.paymentFragment -> text = getString(R.string.payment)
}
}
}
在您的 textView 中添加此行
android:layout_centerHorizontal = "true"
这将解决您的问题
尝试删除android:layout_marginEnd="?android:attr/actionBarSize"并使用alignParentLeft=true或 layout_margenStart。 希望它会奏效。
将您的工具栏文本边距修改为更小的值。
@+id/toolbar_text_view
android:layout_marginEnd="?android:attr/actionBarSize"
至
android:layout_marginEnd="5dp"
你必须约束布局
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.ui.zzzzActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/toolbar_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="?android:attr/actionBarSize"
android:textSize="@dimen/tulisan20sp"
tools:text="Toolbar"
android:textColor="@color/colorwhite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/total_job_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/toolbar_text_view"
android:layout_marginTop="@dimen/dimen8dp"
android:text="1425"
android:textColor="@color/colorwhite"
android:layout_marginEnd="@dimen/dimen40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
更新,如果您希望在片段中不可见的数字,您可以设置可见:不可见和始终居中的文本:
您可以以编程方式设置:
textview.setVisible(View.INVISIBLE)
别
textview.setVisible(View.GONE)
如果你想设置可见:
textview.setVisible(View.VISIBLE)
从 TextView 中删除行:-
android:layout_marginEnd="?android:attr/actionBarSize"
在Relativelayout中添加这一行:-
android:layout_marginStart="?android:attr/actionBarSize"
android:layout_marginEnd="?android:attr/actionBarSize
希望它会奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.