无法将视图放置在RecyclerView下方 [英] Unable to place view below RecyclerView

查看:80
本文介绍了无法将视图放置在RecyclerView下方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Adview放在RecyclerView下方时遇到麻烦.当前RecyclerView占据了其余的布局,并且Adview没有显示.我在网上找到的所有解决方案都没有解决我的问题.通常layout_weightlayout_below可以解决问题,但不是这种情况.

I'm having trouble placing an Adview below a RecyclerView. Currently the RecyclerView is taking up the rest of the layout and the Adview isn't being shown. None of the solutions I have found online have solved my issue. Usually layout_weight or layout_below solves the problem but it isn't in this case.

XML

<RelativeLayout 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"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#7c7c7c"
        android:focusable="true"
        android:focusableInTouchMode="true">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relContainer"
            android:background="@drawable/bordershadow2"
            android:paddingBottom="17dp"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="10dp" />


        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/recycler_view"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>

        <RelativeLayout
            android:id="@+id/relContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bordershadow"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

           <TableLayout
                android:id="@+id/purchaseTableLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="7dp"
                android:paddingTop="7dp"
                android:layout_alignParentTop="true"
                android:stretchColumns="0,1,2">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:weightSum="3">

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center_vertical"
                        android:paddingLeft="2dp"
                        android:singleLine="true"
                        android:textColor="#000"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center_vertical"
                        android:paddingLeft="2dp"
                        android:singleLine="true"
                        android:textColor="#000"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <Spinner
                        android:id="@+id/typeSpinner"
                        style="style/Theme.Material"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center_vertical"
                        android:entries="@array/type_array"
                        android:textSize="@dimen/title_size" />
                </TableRow>
            </TableLayout>

            <EditText
                android:id="@+id/searchEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/purchaseTableLayout"
                android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'() "
                android:hint="Search"
                android:imeOptions="actionDone"
                android:singleLine="true" />

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/searchEditText"
                android:paddingBottom="7dp"
                android:paddingTop="7dp"
                android:stretchColumns="0,1,2">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:weightSum="3.0">

                    <Spinner
                        android:id="@+id/sortRaceSpinner"
                        style="style/Theme.Material"
                        android:paddingTop="5dp"
                        android:paddingBottom="10dp"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1.0"
                        android:entries="@array/sort_race_array"
                        android:textSize="@dimen/title_size" />

                    <Spinner
                        android:id="@+id/sortAffinitySpinner"
                        style="style/Theme.Material"
                        android:paddingTop="5dp"
                        android:paddingBottom="10dp"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1.0"
                        android:entries="@array/sort_affinity_array"
                        android:textSize="@dimen/title_size" />

                    <Spinner
                        android:id="@+id/sortSpinner"
                        style="style/Theme.Material"
                        android:paddingTop="5dp"
                        android:paddingBottom="10dp"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1.0"
                        android:entries="@array/sort_array"
                        android:textSize="@dimen/title_size" />
                </TableRow>
            </TableLayout>
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

推荐答案

我重写了布局文件,将recycleview封装在linearlayout内

I rewrote your layout file, The recycleview is wrapped inside a linearlayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
   android:id="@+id/wrapper"
   android:layout_width="match_parent"
   android:layout_height="60dp"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   android:layout_marginBottom="12dp"
   android:orientation="vertical">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:clipToPadding="false"
       android:paddingBottom="20dp"
       android:paddingLeft="@dimen/activity_horizontal_margin"
       android:paddingRight="@dimen/activity_horizontal_margin"
       android:paddingTop="10dp" />

   </LinearLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="60dp"
    android:layout_below="@+id/wrapper"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>


<RelativeLayout
    android:id="@+id/relContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/adView"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TableLayout
        android:id="@+id/purchaseTableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:layout_alignParentTop="true"
        android:stretchColumns="0,1,2">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="2dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="2dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textSize="24sp"
                android:textStyle="bold" />

            <Spinner
                android:id="@+id/typeSpinner"
                style="style/Theme.Material"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical" />
        </TableRow>
    </TableLayout>

    <EditText
        android:id="@+id/searchEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/purchaseTableLayout"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'() "
        android:hint="Search"
        android:imeOptions="actionDone"
        android:singleLine="true" />

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/searchEditText"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:stretchColumns="0,1,2">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3.0">

            <Spinner
                android:id="@+id/sortRaceSpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>

            <Spinner
                android:id="@+id/sortAffinitySpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>

            <Spinner
                android:id="@+id/sortSpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

结果如下所示

如果回收视图的内容将超出显示高度,则应考虑使用android:layout_weight将布局划分为多个分区.

If your recycleview content will extend beyond the display height then you should consider dividing your layout into partitions with android:layout_weight.

更新

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/relContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TableLayout
        android:id="@+id/purchaseTableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:layout_alignParentTop="true"
        android:stretchColumns="0,1,2">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="2dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="2dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textSize="24sp"
                android:textStyle="bold" />

            <Spinner
                android:id="@+id/typeSpinner"
                style="style/Theme.Material"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical" />
        </TableRow>
    </TableLayout>

    <EditText
        android:id="@+id/searchEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/purchaseTableLayout"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'() "
        android:hint="Search"
        android:imeOptions="actionDone"
        android:singleLine="true" />

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/searchEditText"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:stretchColumns="0,1,2">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3.0">

            <Spinner
                android:id="@+id/sortRaceSpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>

            <Spinner
                android:id="@+id/sortAffinitySpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>

            <Spinner
                android:id="@+id/sortSpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relContainer"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" />

</LinearLayout>

<LinearLayout
    android:id="@+id/wrappers"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

</LinearLayout>

这篇关于无法将视图放置在RecyclerView下方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆