【问题标题】:Collapse layout in a fragment under toolbar在工具栏下的片段中折叠布局
【发布时间】:2018-08-17 10:28:50
【问题描述】:

以下是我在Fragment 中的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragments.TAClaimFragment"
    android:background="@drawable/main_gradient"
    android:orientation="vertical">
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       app:layout_anchor="@id/parent"
       app:layout_anchorGravity="top">
       <android.support.v7.widget.SearchView
           android:layout_width="0dp"
           android:layout_weight="2"
           android:layout_height="?actionBarSize"
           android:layoutDirection="rtl"
           android:id="@+id/ta_search"
           android:animateLayoutChanges="true"
           app:searchIcon="@drawable/ic_search"
           app:closeIcon="@drawable/ic_close"
           android:tooltipText="tooltip"
           app:searchHintIcon="@drawable/ic_search"
           app:queryHint="@string/search_hint"
           />
       <ImageView
           android:layout_width="0dp"
           android:id="@+id/ta_menu"
           android:padding="16dp"
           android:layout_weight="0.3"
           android:layout_height="50dp" />

   </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ta_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

RecyclerView 在线性布局中在其上方有一个SearchView 和一个ImageView 我希望线性布局在包含片段的Activity 所具有的工具栏下消失。我对协调器布局或折叠工具栏布局不够熟悉,但我想解决方案在于使用这两者。

我该怎么做?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我希望线性布局在工具栏下消失 包含该片段的Activity。

    如果您通过FrameLayout(或其他)显示Fragment,请显示Fragment,如下布局:(FrameLayoutNestedScrollView 是可滚动的布局)

    <CoordinatorLayout>
    
        <AppBarLayout>
          <Toolbar/>
        </AppBarLayout>
    
        <NestedScrollView>
    
        <FrameLayout />
    
        </NestedScrollView>
    </CoordinatorLayout>
    

    然后你的Fragment 代码(就像你当前的布局代码)在另一个名为 myfragmentlayout.xml 的布局中。

    所以,在这里,如果你想隐藏LinearLayout,当你滚动RecyclerView 时它就会消失。或者,您可以创建一个包含图片的CollpasingToolbarLayout,然后如果滚动内容,它将消失。

    CollapsingToolbarLayout 的示例,内部带有 ImageView。 (还有更多关于CollapsingToolbarLayout 标签的内容!):

    How to add an ImageView with the title in collapsingtoolbarlayout in Android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      相关资源
      最近更新 更多