如何在包含约束布局的工具栏中心放置视图?

时间:2019-05-08 13:19:04

标签: android xml android-layout android-toolbar

我在工具栏内有一个约束布局。我在约束布局中有一个文本视图。我将文本视图设置在中间,它应该看起来像这样

enter image description here

但是,如果该工具栏的右侧有一个菜单项,则文本视图不再像这样居中

enter image description here

那么,即使我有这样的菜单项,如何使文本视图仍居中?

这是我使用的XML:

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Activities.MainActivity" android:id="@+id/constraintLayout_main_activity">


    <androidx.appcompat.widget.Toolbar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:theme="?attr/actionBarTheme"
            android:minHeight="?attr/actionBarSize"
            android:id="@+id/top_toolbar"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            android:elevation="4dp"
            android:contentInsetLeft="0dp"
            android:contentInsetStart="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            android:contentInsetRight="0dp"
            android:contentInsetEnd="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetEnd="0dp">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                tools:context=".Fragments.Search.SearchFragment"
                android:id="@+id/constraintLayout_toolbar" tools:layout_editor_absoluteY="8dp"
                tools:layout_editor_absoluteX="16dp">


            <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:id="@+id/base_searchView_toolbar_main_activity"
                    android:background="@drawable/round_outline_search_view" app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginEnd="16dp"
                    app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"
                    android:layout_marginTop="8dp" android:layout_marginBottom="8dp"
                    app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="1.0"
                    android:layout_marginStart="16dp"/>

            <ImageView
                    android:src="@drawable/ic_search"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView_search_icon_main_activity"
                    app:layout_constraintStart_toStartOf="@+id/base_searchView_toolbar_main_activity"
                    android:layout_marginStart="8dp"
                    app:layout_constraintTop_toTopOf="@+id/base_searchView_toolbar_main_activity"
                    app:layout_constraintBottom_toBottomOf="@+id/base_searchView_toolbar_main_activity"/>

            <EditText
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:inputType="textPersonName"
                    android:ems="10"
                    android:id="@+id/editText_search_keyword_main_activity"
                    android:hint="Cari Acara, Ustadz atau Lokasi" android:textSize="12sp"
                    app:layout_constraintStart_toEndOf="@+id/imageView_search_icon_main_activity"
                    android:layout_marginStart="4dp" android:layout_marginEnd="8dp"
                    app:layout_constraintEnd_toEndOf="@+id/base_searchView_toolbar_main_activity"
                    app:layout_constraintTop_toTopOf="@+id/base_searchView_toolbar_main_activity"
                    app:layout_constraintBottom_toBottomOf="@+id/base_searchView_toolbar_main_activity"
                    android:background="@android:color/transparent"
                    android:imeOptions="actionSearch"
                    android:maxLines="1"/>
            <TextView
                    android:text="TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="constraintLayout_main_activity"
                    android:layout_marginStart="8dp" android:layout_marginTop="8dp"
                    app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
                    app:layout_constraintBottom_toBottomOf="parent" android:id="@+id/destination_label_text_view"/>


        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.appcompat.widget.Toolbar>


    <fragment
            android:id="@+id/nav_host_fragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
            app:layout_constraintTop_toBottomOf="@+id/top_toolbar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/main_graph"
            app:defaultNavHost="true"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="0dp"
            android:layout_height="50dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:background="@color/colorPrimary"
            app:itemIconTint="@color/bottom_navigation_color"
            app:itemTextColor="@color/bottom_navigation_color"
            app:menu="@menu/bottom_navigation_menu"
            app:labelVisibilityMode="unlabeled"
            android:id="@+id/bottom_nav"/>


    <ProgressBar
            style="?android:attr/progressBarStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar_main_activity"
            app:layout_constraintTop_toBottomOf="@id/top_toolbar"
            app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
            app:layout_constraintStart_toStartOf="@+id/nav_host_fragment"
            app:layout_constraintEnd_toEndOf="@+id/nav_host_fragment"
            android:visibility="gone"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我还尝试了另一种方法,即在工具栏外部设置文本视图,然后将其放置在工具栏上方,但是文本不会像这样显示

enter image description here

那么,即使工具栏内有菜单项,如何将视图设置在工具栏的中央?

0 个答案:

没有答案