簡體   English   中英

在折疊的折疊工具欄布局中為標題添加邊距

[英]Add margin to title in collapsed Collapsing Toolbar Layout

我正在嘗試在折疊的 state 中的標題開始的位置添加邊距。 我有一個 Button 覆蓋標題開始的位置:

在此處輸入圖像描述

我嘗試在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:fitsSystemWindows="true"

  • paddingLeft添加到工具欄

<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: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"
            app:collapsedTitleGravity="left"
            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:scaleType="centerCrop"
                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:paddingLeft="42dp"
                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>

你沒有遵循正確的模式。 協調器布局始終具有第一個子 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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM