【问题标题】:How can i put the element RecyclerView under the element Toolbar in the Android Layout file?如何将元素 RecyclerView 放在 Android 布局文件中的元素工具栏下?
【发布时间】:2018-02-23 05:40:01
【问题描述】:

嘿,我希望在我的 android 应用程序中,recyclerlistview 和 floatingActionButton 不会剪切工具栏。这是我的布局:

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

            <include
                layout="@layout/toolbar_task_layout">
            </include>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView_task"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                app:layout_anchor="@id/recyclerView_task">
            </android.support.v7.widget.RecyclerView>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/floatingActionButton_task_openDialog_"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:layout_margin="16dp"
                android:src="@drawable/ic_add_black_36dp"
                app:layout_anchor="@id/recyclerView_task"
                android:clickable="true"
                app:fabSize="normal"
                app:layout_anchorGravity="bottom|right|end"/>


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

这里你可以看到问题:

更新:

这是工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                   xmlns:app="http://schemas.android.com/apk/res-auto"
                                   android:layout_width="match_parent"
                                   android:layout_height="wrap_content"
                                   android:id="@+id/toolbar_task"
                                   android:layout_alignParentTop="true"
                                   android:background="?attr/colorPrimary"
                                   android:elevation="6dp"
                                   android:minHeight="?attr/actionBarSize"
                                   app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                                   app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0 items selected"
            android:id="@+id/counter_task"
            android:textSize="18dp"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

【问题讨论】:

  • 分享 Toolbar_task_layout 文件
  • 查看更新:) ...
  • recyclerview 中使用layout_below 并给出toolbar_task_layoutid
  • 我不能使用 layout_below ;(
  • 让您增加margin_top 的值并进行测试。我认为 recyclerview 从工具栏开始。

标签: android android-layout layout android-recyclerview floating-action-button


【解决方案1】:

尝试替换您的&lt;include layout="@layout/toolbar_task_layout"&gt;&lt;/include&gt;

用这个:

<include layout="@layout/toolbar_task_layout" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

编辑:我假设您还希望您的 FAB 位于右下角,因为这是常态,请尝试在您的 FAB 中设置 android:layout_alignParentBottom="true"android:layout_alignParentRight="true" 并使 RecyclerViews 高度 match_parent

【讨论】:

  • 工具栏布局应该不重要,您是否已经尝试过修复?
  • 啊,我明白了,为什么现在您使用的是协调器布局,这对我不注意是不好的,仅将包含中的 layout_below 设置为 recyclerview 的 id 是否有效?
【解决方案2】:

试试这个把你的toolbar android:layout_height 变成android:layout_height="?attr/actionBarSize"

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

【讨论】:

    【解决方案3】:

    使用下面的代码:

    <android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <include layout="@layout/toolbar_task_layout"/>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_task"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp"
            app:layout_anchor="@id/recyclerView_task"
            android:layout_below="@+id/toolbar_task"/>
    
    </RelativeLayout>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton_task_openDialog_"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@mipmap/ic_launcher"
        app:layout_anchor="@id/recyclerView_task"
        android:clickable="true"
        app:fabSize="normal"
        app:layout_anchorGravity="bottom|right|end"/></android.support.design.widget.CoordinatorLayout>
    

    我希望这会有所帮助.....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-05
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多