工具栏上的标题不会随着折叠的工具栏布局而扩展和折叠,只是停留在导航抽屉按钮旁边。 正在折叠的作品,我只需要标题即可使其与工具栏的扩展和折叠紧密相关,任何人都可以在这个问题上提供帮助
我尝试使用以下代码以编程方式设置标题,但无济于事。变量已使用findViewById CollapsingToolBarLayout.setTitle(“ title);
绑定到布局<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="left">
<RelativeLayout
style="@style/MatchParentMatchParent"
android:background="@color/color_white">
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout_collapse"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true">
<androidx.appcompat.widget.Toolbar
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"
style="@style/ToolBar"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentMatchParent">
<ImageView
android:id="@+id/home"
style="@style/WrapContentWrapContent"
android:layout_centerVertical="true"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="@drawable/btn_back_selector"
android:contentDescription="@string/desc_back_button"
android:padding="15dp"
android:src="@drawable/vd_nav_drawer"
app:layout_marginLeftPercent="4%"/>
<TextView
android:id="@+id/text_toolbar_title"
style="@style/ToolBarTitle"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/home"
android:layout_marginLeft="@dimen/dimen_default_padding"
android:gravity="left"/>
<RelativeLayout
android:id="@+id/relative_toolbar_buttons"
style="@style/WrapContentWrapContent"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:importantForAccessibility="no"
android:scaleType="center"
app:layout_marginRightPercent="4%">
<ImageView
android:id="@+id/image_toolbar_primary_button"
style="@style/WrapContentWrapContent"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:padding="@dimen/dimen_small_padding"
android:visibility="invisible"/>
<ImageView
android:id="@+id/image_toolbar_secondary_button"
style="@style/WrapContentWrapContent"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="@dimen/dimen_small_padding"
android:padding="@dimen/dimen_small_padding"
android:visibility="invisible"
android:src="@drawable/vd_outline_info_24dp"/>
<TextView
android:id="@+id/text_toolbar_button"
style="@style/MatchParentMatchParent"
android:gravity="center"
android:textAllCaps="true"
android:textColor="@color/color_toolbar_text"
android:textSize="14sp"
android:textStyle="bold"
android:visibility="invisible"/>
</RelativeLayout>
</androidx.percentlayout.widget.PercentRelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<FrameLayout
android:id="@+id/fragment_container"
style="@style/MatchParentMatchParent"
/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/left_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/color_white"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp">
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentMatchParent">
<TextView
style="@style/WrapContentWrapContent"
android:fontFamily="sans-serif"
android:paddingStart="50dp"
android:paddingEnd="50dp"
android:text="@string/title_welcome"
android:textColor="@color/color_dark"
android:textSize="25sp"
app:layout_marginTopPercent="10%"/>
<TextView
style="@style/WrapContentWrapContent"
android:layout_below="@id/text_nav_drawer_name"
android:fontFamily="sans-serif"
android:paddingStart="50dp"
android:paddingEnd="50dp"
android:text="@string/msg_available_passes"
android:textColor="@color/color_blue"
android:textSize="12sp"
app:layout_marginBottomPercent="6%"
app:layout_marginTopPercent="2%" />
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentZeroHeight"
android:layout_below="@id/text_nav_drawer_message"
android:importantForAccessibility="no"
app:layout_heightPercent="7.1%"
android:visibility="gone">
<View
style="@style/MatchParentZeroHeight"
android:background="@color/color_grey"
app:layout_heightPercent="1%"/>
<TextView
style="@style/NavMenuTextButton"
/>
<TextView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignParentEnd="true"
android:layout_marginTop="14.5dp"
android:layout_marginEnd="90dp"
android:background="@drawable/textview_round_secondary"
android:fontFamily="sans-serif"
android:gravity="center"
android:textColor="@color/color_white"
android:textSize="10sp"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentZeroHeight"
android:importantForAccessibility="no"
app:layout_heightPercent="7.1%"
android:visibility="gone">
<View
style="@style/MatchParentZeroHeight"
android:background="@color/color_grey"
app:layout_heightPercent="1%"/>
<TextView
style="@style/NavMenuTextButton"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentZeroHeight"
android:layout_below="@id/percent_menu_buy_passes"
android:importantForAccessibility="no"
app:layout_heightPercent="7.1%"
android:visibility="gone">
<View
android:id="@+id/view_menu_divider_planner"
style="@style/MatchParentZeroHeight"
android:background="@color/color_grey"
app:layout_heightPercent="1%"/>
<TextView
style="@style/NavMenuTextButton"
android:layout_below="@id/view_menu_divider_planner"
android:text="@string/action_trip_planner"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentZeroHeight"
android:layout_below="@id/percent_menu_planner"
android:importantForAccessibility="no"
app:layout_heightPercent="7.1%"
android:visibility="gone">
<View
style="@style/MatchParentZeroHeight"
android:background="@color/color_grey"
app:layout_heightPercent="1%"/>
<TextView
style="@style/NavMenuTextButton"
android:layout_below="@id/view_menu_divider_my_trips"
android:contentDescription="@string/desc_my_trips_button"
android:text="@string/action_my_trips"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentZeroHeight"
android:layout_below="@id/percent_menu_my_trips"
android:importantForAccessibility="no"
app:layout_heightPercent="7.1%"
android:visibility="gone">
<View
style="@style/MatchParentZeroHeight"
android:background="@color/color_grey"
app:layout_heightPercent="1%"/>
<TextView
style="@style/NavMenuTextButton"
android:layout_below="@id/view_menu_divider_favorites"
android:text="@string/action_favorites"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentZeroHeight"
android:layout_below="@id/percent_menu_favorites"
android:importantForAccessibility="no"
app:layout_heightPercent="7.1%"
android:visibility="gone">
<View
style="@style/MatchParentZeroHeight"
android:background="@color/color_grey"
app:layout_heightPercent="1%"/>
<TextView
style="@style/NavMenuTextButton"
android:layout_below="@id/view_menu_divider_stops_nearby"
android:contentDescription="@string/desc_nearby_stops_button"
android:text="@string/title_nearby_stops"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentZeroHeight"
android:layout_below="@id/percent_menu_stops_nearby"
android:importantForAccessibility="no"
app:layout_heightPercent="7.1%">
<View
style="@style/MatchParentZeroHeight"
android:background="@color/color_grey"
app:layout_heightPercent="1%"/>
<TextView
style="@style/NavMenuTextButton"
android:layout_below="@id/view_menu_divider_settings"
android:text="@string/action_settings"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentWrapContent"
android:importantForAccessibility="no"
android:visibility="gone"
tools:visibility="visible"
app:layout_heightPercent="7.1%"
app:layout_marginTopPercent="69.0%">
<ImageView
android:layout_width="100dp"
android:layout_height="25dp"
android:layout_alignParentStart="true"
android:layout_marginStart="50dp"
android:layout_marginTop="8dp"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentWrapContent"
android:importantForAccessibility="no"
android:visibility="gone"
tools:visibility="visible"
app:layout_heightPercent="7.1%">
<ImageView
android:layout_width="110dp"
android:layout_height="25dp"
android:layout_alignParentStart="true"
android:layout_marginStart="50dp"
android:layout_marginTop="8dp"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<androidx.percentlayout.widget.PercentRelativeLayout
style="@style/MatchParentWrapContent"
android:importantForAccessibility="no"
android:visibility="gone"
tools:visibility="visible"
app:layout_heightPercent="7.1%"/>
<ImageView
android:layout_width="80dp"
android:layout_height="25dp"
android:layout_alignParentStart="true"
android:layout_marginStart="50dp"
android:layout_marginTop="8dp"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
android:src="@drawable/black_link"
/>
<View
style="@style/MatchParentZeroHeight"
android:layout_alignParentBottom="true"
android:background="@color/color_grey"
app:layout_heightPercent="1%"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
<View
android:id="@+id/view_menu_divider_settings_below"
style="@style/MatchParentZeroHeight"
android:layout_below="@id/percent_menu_settings"
android:background="@color/color_grey"
app:layout_heightPercent="0.1%"/>
<TextView
style="@style/WrapContentWrapContent"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:textColor="@color/color_blue"
android:textSize="13sp"
app:layout_marginLeftPercent="15%"
app:layout_marginTopPercent="92.5%"/>
</androidx.percentlayout.widget.PercentRelativeLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
答案 0 :(得分:0)
您的xml代码中有更多视图,我需要花一些时间来修改它们,而不是我会提示您,因此请尝试对其进行修改。
xml代码
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.NewsReaderActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavi"
/>
<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"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="bottom"
app:expandedTitleMarginEnd="@dimen/activity_horizontal_margin"
app:expandedTitleMarginStart="@dimen/activity_horizontal_margin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/toolbar_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="@null"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/dabdamis" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
将其放入您的onCreate
方法
setSupportActionBar(toolbar)
还将以下代码用作工具栏的标题
//set title of the toolbar
collapsing_toolbar.setTitle= "Your Title"
collapsing_toolbar
是我的CollapsinToolbar的ID,请使用findViewById