Android 带工具栏的嵌套滚动视图

Android 带工具栏的嵌套滚动视图,android,android-recyclerview,scrollview,toolbar,Android,Android Recyclerview,Scrollview,Toolbar,所以我为AppTheme NoActionBar设置了styles.xml,因为我只想在一些活动中使用工具栏,而且我还创建了包含两个项目的post_details_菜单。但工具栏根本不显示 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 最后,这是我的ActivityPostDetails布局: <androidx.core.widget.NestedScrollView xmlns:and

所以我为AppTheme NoActionBar设置了styles.xml,因为我只想在一些活动中使用工具栏,而且我还创建了包含两个项目的post_details_菜单。但工具栏根本不显示

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
最后,这是我的ActivityPostDetails布局:

<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"
tools:context=".PostDetailActivity"
android:background="#fff">

   <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

       <ImageView ... />

       <TextView ... />

       <androidx.recyclerview.widget.RecyclerView .../>

   </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

太简单了。在要显示工具栏的XML文件中设置应用程序栏布局

检查以下XML代码:-

<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"
tools:context=".PostDetailActivity"
android:background="#fff">

   <androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">

         <com.google.android.material.appbar.AppBarLayout
              android:id="@+id/appBarLayout"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:theme="@style/AppTheme.AppBarOverlay"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent">

                 <androidx.appcompat.widget.Toolbar 
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     xmlns:app="http://schemas.android.com/apk/res-auto"
                     android:id="@+id/toolbar"
                     android:layout_width="match_parent"
                     android:layout_height="?attr/actionBarSize"
                     android:background="@color/colorPrimary"
                     android:theme="@style/AppTheme.ActionBar"
                     app:navigationIcon="@drawable/ic_back"
                  app:subtitleTextAppearance="@style/CustomSubTitleTextAppearance"
                     app:titleTextColor="@color/black" />

                    </com.google.android.material.appbar.AppBarLayout>

                    <ImageView ... />

                    <TextView ... />

                    <androidx.recyclerview.widget.RecyclerView .../>

                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.core.widget.NestedScrollView>

类似这样的Xml结构应该可以解决这个问题

<LinearLayout>
<Toolbar/>
<NestedScrollView>
//one parent view inside nestedscrollview
<AnylayoutType>
// other required views
</AnylayoutType>
</NestedScrollView>
<LinearLayout/>

//嵌套ScrollView中的一个父视图
//其他所需意见
使用这种方法,工具栏将不会移动,菜单选项将始终可用,嵌套的sroll视图将根据需要工作

如果确定视图层次结构没有嵌套,最好在嵌套的scrollview中使用LinearLayout

<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"
tools:context=".PostDetailActivity"
android:background="#fff">

   <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

       <ImageView ... />

       <TextView ... />

       <androidx.recyclerview.widget.RecyclerView .../>

   </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>
<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"
tools:context=".PostDetailActivity"
android:background="#fff">

   <androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">

         <com.google.android.material.appbar.AppBarLayout
              android:id="@+id/appBarLayout"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:theme="@style/AppTheme.AppBarOverlay"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent">

                 <androidx.appcompat.widget.Toolbar 
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     xmlns:app="http://schemas.android.com/apk/res-auto"
                     android:id="@+id/toolbar"
                     android:layout_width="match_parent"
                     android:layout_height="?attr/actionBarSize"
                     android:background="@color/colorPrimary"
                     android:theme="@style/AppTheme.ActionBar"
                     app:navigationIcon="@drawable/ic_back"
                  app:subtitleTextAppearance="@style/CustomSubTitleTextAppearance"
                     app:titleTextColor="@color/black" />

                    </com.google.android.material.appbar.AppBarLayout>

                    <ImageView ... />

                    <TextView ... />

                    <androidx.recyclerview.widget.RecyclerView .../>

                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.core.widget.NestedScrollView>
toolbar = findViewById(R.id.toolbar)
toolbar!!.setNavigationIcon(R.drawable.ic_back)
if (toolbar != null) {
   setSupportActionBar(toolbar)
}
<LinearLayout>
<Toolbar/>
<NestedScrollView>
//one parent view inside nestedscrollview
<AnylayoutType>
// other required views
</AnylayoutType>
</NestedScrollView>
<LinearLayout/>