20

我正在尝试重新创建 Airbnb Android 应用程序中的搜索框。所以我将CoorinatorLayoutToolbarRecyclerView一起使用。

但是,当我在 Coordinator 中插入除这两件事之外的其他内容时,它不会显示出来。这是我的代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/slidingLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/red"
        app:layout_scrollFlags="scroll|enterAlways" />

</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="20dp"
    android:background="@drawable/rounded_background"
    android:orientation="horizontal"
    android:padding="6dp"
    app:layout_scrollFlags="scroll|enterAlways">

    <EditText
        android:id="@+id/search"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="6"
        android:background="@null"
        android:fontFamily="sans-serif-light"
        android:hint="Unesite grad"
        android:paddingLeft="16dp"
        android:paddingStart="16dp" />

    <ImageView
        android:id="@+id/cancelSearch"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:padding="10dp"
        android:src="@drawable/ic_cancel" />
</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/appbar"
    android:background="#ffffff"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

但实际上,如果这能奏效,我将很难将搜索框放在 RecyclerView 上方。

我试图将所有内容都放在RelativeLayout中,但这没有用。

这也是我正在尝试制作的图片 在此处输入图像描述

编辑:

这是RelativeLayout的代码

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/slidingLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <android.support.design.widget.AppBarLayout
           android:id="@+id/appbar"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
           android:background="#00000000">

           <android.support.v7.widget.Toolbar
               android:id="@+id/toolbar"
               android:layout_width="match_parent"
               android:layout_height="?attr/actionBarSize"
               android:background="@color/red"
               app:layout_scrollFlags="scroll|enterAlways"/>
   </android.support.design.widget.AppBarLayout>

   <RelativeLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
       <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginLeft="50dp"
          android:layout_marginRight="50dp"
          android:layout_marginTop="20dp"
          android:background="@drawable/rounded_background"
          android:orientation="horizontal"
          android:padding="6dp"
          app:layout_scrollFlags="scroll|enterAlways">

          <EditText
              android:id="@+id/search"
              android:layout_width="0dp"
              android:layout_height="fill_parent"
              android:layout_weight="6"
              android:background="@null"
              android:fontFamily="sans-serif-light"
              android:hint="Unesite grad"
              android:paddingLeft="16dp"
              android:paddingStart="16dp" />

          <ImageView
              android:id="@+id/cancelSearch"
              android:layout_width="0dp"
              android:layout_height="40dp"
              android:layout_weight="1"
              android:padding="10dp"
              android:src="@drawable/ic_cancel" />
      </LinearLayout>
      <android.support.v7.widget.RecyclerView
           android:id="@+id/recyclerView"
           android:layout_width="match_parent"
           android:layout_height="fill_parent"
           android:layout_below="@id/appbar"
           android:background="#ffffff"
           app:layout_behavior="@string/appbar_scrolling_view_behavior" />
   </RelativeLayout>

4

3 回答 3

28

我试图做一些类似的事情,但在屏幕底部有一个广告横幅。我的解决方案是这样包装RelativeLayout外部CoordinatorLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <android.support.design.widget.CoordinatorLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_above="@+id/ad_view">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          app:layout_scrollFlags="scroll|enterAlways"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

  </android.support.design.widget.CoordinatorLayout>

  <com.google.android.gms.ads.AdView
      android:id="@+id/ad_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_alignParentBottom="true"
      android:layout_gravity="center|bottom"
      app:adSize="SMART_BANNER"
      app:adUnitId="@string/admob_id" />

</RelativeLayout>

在内部滚动时工具栏会正确隐藏,RecyclerView并且一切似乎都运行良好。我想如果你遵循类似的原则,你应该会很好。

于 2015-10-31T13:51:12.323 回答
0

如果要删除状态栏白色,则必须删除下一行

<item name="android:statusBarColor">@android:color/transparent</item>`

v21/styles.xml

例如,谢谢。

于 2017-03-07T16:58:00.740 回答
0
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/slidingLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/red"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/rounded_background"
            android:orientation="horizontal"
            android:padding="6dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <EditText
                android:id="@+id/search"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="6"
                android:background="@null"
                android:fontFamily="sans-serif-light"
                android:hint="Unesite grad"
                android:paddingLeft="16dp"
                android:paddingStart="16dp"/>

            <ImageView
                android:id="@+id/cancelSearch"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:padding="10dp"
                android:src="@drawable/ic_cancel" />
        </LinearLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/appbar"
        android:background="#ffffff"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

检查这是否适合您。

于 2017-09-22T05:53:36.087 回答