在折叠的工具栏布局中为标题添加页边空白

问题描述 投票:0回答:1

我正在尝试在折叠状态下我的标题开始处添加边距。我有一个按钮,它覆盖了标题开始的位置:

enter image description here

我曾尝试在app:collapsedTitleTextAppearance中使用左边界的样式,但这也不起作用。以下是我的.xml文件。 (请注意,后退按钮是文本视图,而不是操作栏。)标题是通过编程方式添加的。

<style name ="collapsingToolbarTextCollapsed">
        <item name="android:layout_marginLeft">42dp</item>
        <item name="android:paddingLeft">42dp</item>
        <item name="paddingStart">42dp</item>
    </style>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <TextView
        android:id="@+id/profile_main_expanded_back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:elevation="7dp"
        android:background="@drawable/back_button"
        android:foreground="@drawable/back_button" />

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:collapsedTitleTextAppearance="@style/collapsingToolbarTextCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <ImageView
                android:id="@+id/profile_avatar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="profile picture"
                android:fitsSystemWindows="true"
                android:scaleType="fitXY"
                android:src="@drawable/placeholder_user"
                app:layout_collapseMode="parallax"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/htab_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="top"
                android:layout_marginBottom="4dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
android android-collapsingtoolbarlayout
1个回答
0
投票

您没有遵循正确的模式。协调器布局始终具有第一个孩子AppBarLayout和第二个孩子带有行为的ScrollView / NestedScrollView / RecyclerView中的任何一个。

不要为后退按钮使用单独的视图,可以使用以下代码启用工具栏的后退按钮

toolbar = (Toolbar) findViewById(R.id.htab_toolbar); 
setSupportActionBar(toolbar);

//this line shows back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

并删除您的文本,您使用该文本来显示如下所示的后退按钮。

<androidx.coordinatorlayout.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"

      app:collapsedTitleTextAppearance="@style/collapsingToolbarTextCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/profile_avatar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="profile picture"
            android:fitsSystemWindows="true"
            android:scaleType="fitXY"
            android:src="@drawable/placeholder_user"
            app:layout_collapseMode="parallax"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/htab_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="top"
            android:layout_marginBottom="4dp"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout> 
© www.soinside.com 2019 - 2024. All rights reserved.