简体   繁体   English

Android:RelativeLayout上RecyclerView上方的TextView

[英]Android: TextView Above RecyclerView on RelativeLayout

Okay, I need some help because this is driving me insane. 好的,我需要一些帮助,因为这使我发疯。 I've gone through every post I could find on StackOverflow about this and I have tried EVERYTHING. 我遍历了我在StackOverflow上可以找到的所有关于此的文章,并且我尝试了一切。 The Preview, I should mention, looks perfect. 我应该提到的预览版看起来很完美。 But when I run the app the TextView simply isn't there. 但是,当我运行该应用程序时,TextView根本不存在。

I've followed the suggestion that said to make the RelativeLayout "focusable." 我遵循了使RelativeLayout具有“可聚焦性”的建议。 I tried the one that said to put the TextView under the RecyclerView. 我尝试了一种将TextView放在RecyclerView下的方法。 I tried the one that goes something like "LayoutAbove=@recyclerviewid". 我尝试了类似“ LayoutAbove = @ recyclerviewid”的程序。

I have literally tried everything that I found on StackOverflow and nothing has worked and I can't understand why it would work in the preview but not in the app. 我已经尝试了我在StackOverflow上找到的所有内容,但没有任何效果,我不明白为什么它会在预览中起作用,但在应用程序中不起作用。 I don't understand that at all. 我一点都不明白。 Anyway! 无论如何! The code: 编码:

<?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>

EDIT: As per TakeInfo's answer... (and this is very important and I did not know it...) the ONLY reason the code above wasn't working was because I used "tools:text="EVENTS"" which meant that the "text" only gets sent to Android Studio. 编辑:根据TakeInfo的答案...(这是非常重要的,我不知道...)上面的代码不起作用的唯一原因是因为我使用了“ tools:text =” EVENTS“”仅将“文本”发送到Android Studio。

I will of course leave this question up here because I'm sure I'm not the only one who didn't know that. 我当然会把这个问题留在这里,因为我确定我不是唯一一个不知道这一点的人。

This question wasn't as simple as some people may have thought... it actually had nothing to do with the layout... but an error in the code. 这个问题并不像某些人想象的那么简单……它实际上与布局无关……而是代码中的错误。 I think most people just dismissed the question at first glance without really LOOKING at what the problem might be. 我认为大多数人乍一看都忽略了这个问题,而没有真正去看问题可能是什么。 A lesson for us all. 给我们所有人一个教训。 :) :)

Give TextView id attribute like this 像这样给TextView id属性

android:id="@+id/textView"

Then add this attribute in it 然后在其中添加此属性

android:layout_below="@+id/textView" 

in the RecyclerView element 在RecyclerView元素中

Try android:text="EVENTS" instead of tools:text="EVENTS" 尝试使用android:text="EVENTS"而不是tools:text="EVENTS"

when you use 'tools' it only displays text in the android studio. 当您使用“工具”时,它只会在android studio中显示文本。

Give an id to textview and align your recyclerview to below the textview. 给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"/> 

It is very simple, just use LinearLayout with orientation vertical. 这非常简单,只需将LinearLayout与垂直方向一起使用即可。 So that all your views will appear one below another. 这样您的所有视图便会一个接一个地显示。

In your xml, switch places with your TextView and the RecyclerView. 在xml中,使用TextView和RecyclerView切换位置。

The xml is "drawn" from top to bottom, so the TextView will be drawn before and then the recyclerView will be drawn above the TextView. xml是从上到下“绘制”的,因此TextView将先绘制,然后recyclerView将绘制在TextView上方。

Switching them will make the TextView to be drawn after the recycler view. 切换它们将使TextView在回收者视图之后绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM