gpt4 book ai didi

java - 折叠工具栏问题标题停留在屏幕顶部并且不折叠 Android

转载 作者:行者123 更新时间:2023-12-02 03:10:43 24 4
gpt4 key购买 nike

工具栏上的标题不会随着折叠的工具栏布局展开和折叠,它只是固定在抽屉导航按钮旁边。折叠作品我只需要标题通过工具栏的展开和折叠进行动画处理任何人都可以在这个问题上提供帮助

我尝试使用以下代码以编程方式设置标题,但无济于事。变量已通过 findViewById CollapsingToolBarLayout.setTitle("title);

绑定(bind)到布局
<?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>

最佳答案

你的xml代码中有更多的 View ,我需要花时间来修改它们,而不是我会给你一个提示,所以尝试修改它。

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

关于java - 折叠工具栏问题标题停留在屏幕顶部并且不折叠 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56996539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com