简体   繁体   English

自定义工具栏侧 textView 在启用后退按钮或汉堡按钮时分离,位于工具栏标题的末尾

[英]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>

In this project, I use Navigation Component.在这个项目中,我使用导航组件。 I want to show the number in blue border beside the "Job Board".我想在“工作委员会”旁边的蓝色边框中显示数字。 I took a look at this solution.我看了一下这个解决方案。 Toolbar title not in center when Back Button is enable .That solution abled to solve the problem of centering the toolbar text to center but not the Number. 启用后退按钮时工具栏标题不在中心。该解决方案能够解决工具栏文本居中而不是数字居中的问题。 I want this type of output我想要这种类型的 output 在此处输入图像描述

Finally I solved the problem.最后我解决了这个问题。 The main culprit was contentinsetstart which take 72 dp as padding in the beginning of Toolbar which size is 72 dp.罪魁祸首是 contentinsetstart,它在 Toolbar 的开头使用 72 dp 作为填充,大小为 72 dp。 so what I did, I just gave a negative 72 margin in toolbarTextView.所以我做了什么,我只是在toolbarTextView中给出了一个负的72边距。 And everything is running as expected:)一切都按预期运行:)

<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>

and destinationChangeListener looks exactly like和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)
                }
            }

        }

add this line in your textView在您的 textView 中添加此行

android:layout_centerHorizontal = "true"

this will solve your problem这将解决您的问题

try removing android:layout_marginEnd="?android:attr/actionBarSize" and use alignParentLeft=true or layout_margenStart.尝试删除android:layout_marginEnd="?android:attr/actionBarSize"并使用alignParentLeft=true或 layout_margenStart。 hope it will work.希望它会奏效。

Modify your toolbar text margin to something smaller.将您的工具栏文本边距修改为更小的值。

@+id/toolbar_text_view @+id/toolbar_text_view

  android:layout_marginEnd="?android:attr/actionBarSize"

To

 android:layout_marginEnd="5dp"

在此处输入图像描述

you must constraint layout你必须约束布局

<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>

UPDATE if you want the number not visible in fragment you can set visible:invisible and text in always center:更新,如果您希望在片段中不可见的数字,您可以设置可见:不可见和始终居中的文本: 这个xml

you can set programatically:您可以以编程方式设置:

textview.setVisible(View.INVISIBLE)

don't

textview.setVisible(View.GONE)

if you want set visible:如果你想设置可见:

textview.setVisible(View.VISIBLE)

remove line from TextView:-从 TextView 中删除行:-

android:layout_marginEnd="?android:attr/actionBarSize"

Add this line in Relativelayout:-在Relativelayout中添加这一行:-

android:layout_marginStart="?android:attr/actionBarSize"
android:layout_marginEnd="?android:attr/actionBarSize

hope it will work.希望它会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM