【问题标题】:custom toolbar after collapsing app bar in android studio在android studio中折叠应用栏后的自定义工具栏
【发布时间】:2021-08-08 07:49:11
【问题描述】:

我想创建一个这样的布局:

搜索视图在应用栏布局中的位置。当它折叠时,我想像这样更改工具栏:

我已经搜索了所有地方,但我无法提出一个让我满意的解决方案。

谢谢。

【问题讨论】:

    标签: android-studio android-toolbar searchview android-coordinatorlayout android-collapsingtoolbarlayout


    【解决方案1】:

    使用此代码。

    <?xml version="1.0" encoding="utf-8"?>
    <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/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_marginTop="?attr/actionBarSize"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="375dp"
                    android:src="@drawable/yourlogo"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/mToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"/>
    
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:orientation="horizontal">
    
            <androidx.appcompat.widget.SearchView
                android:id="@+id/search_ciew"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginStart="24dp"
                android:layout_marginEnd="24dp"
                android:background="#fff"        
                app:iconifiedByDefault="false"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:queryHint="" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </com.google.android.material.appbar.AppBarLayout>
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraint_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
      </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-22
      • 2016-10-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多