Android:RelativeLayout上RecyclerView上方的TextView

时间:2019-02-28 06:09:25

标签: android android-layout android-recyclerview textview

好的,我需要一些帮助,因为这让我发疯了。我遍历了我在StackOverflow上可以找到的所有关于此的文章,并且我尝试了一切。我应该提到的预览版看起来很完美。但是,当我运行该应用程序时,根本就没有TextView。

我遵循了使RelativeLayout具有“可聚焦性”的建议。我尝试了一种将TextView放在RecyclerView下的方法。我尝试了类似“ LayoutAbove = @ recyclerviewid”的程序。

我确实尝试了在StackOverflow上找到的所有内容,但没有任何效果,我不明白为什么它可以在预览中工作,但在应用程序中不行。我一点都不明白。无论如何!代码:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        tools:text="EVENTS"
        android:textStyle="bold"
        android:paddingTop="10dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="60dp"
        android:id="@+id/events_view"
        >

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



</RelativeLayout>

编辑:按照TakeInfo的回答...(这很重要,我不知道...),上述代码无法正常工作的唯一原因是因为我使用了“ tools:text =“ EVENTS”“表示“文本”仅发送到Android Studio。

我当然会在这里留下这个问题,因为我敢肯定我不是唯一一个不知道这一点的人。

这个问题并不像某些人想象的那么简单...它实际上与布局无关...而是代码错误。我认为大多数人乍一看都忽略了这个问题,而没有真正去看问题可能是什么。给我们所有人一个教训。 :)

4 个答案:

答案 0 :(得分:1)

提供这样的TextView id属性

android:id="@+id/textView"

然后在其中添加此属性

android:layout_below="@+id/textView" 

在RecyclerView元素中

答案 1 :(得分:0)

尝试用android:text="EVENTS"代替tools:text="EVENTS"

当您使用“工具”时,它只会在android studio中显示文本。

为textview提供一个ID,并将您的recyclerview对齐到textview下方。

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="EVENTS"
        android:textStyle="bold"
        android:paddingTop="10dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView"
        android:paddingTop="60dp"
        android:id="@+id/events_view"/>

答案 2 :(得分:0)

这非常简单,只需将LinearLayout与垂直方向一起使用即可。这样您的所有视图都将一个接一个地显示。

答案 3 :(得分:0)

在xml中,使用TextView和RecyclerView切换位置。

xml是从上到下“绘制”的,因此将先绘制TextView,然后将recyclerView绘制在TextView上方。

切换它们将使TextView在回收者视图之后绘制。