简体   繁体   中英

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. The Preview, I should mention, looks perfect. But when I run the app the TextView simply isn't there.

I've followed the suggestion that said to make the RelativeLayout "focusable." I tried the one that said to put the TextView under the RecyclerView. I tried the one that goes something like "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. 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.

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

android:id="@+id/textView"

Then add this attribute in it

android:layout_below="@+id/textView" 

in the RecyclerView element

Try android:text="EVENTS" instead of tools:text="EVENTS"

when you use 'tools' it only displays text in the android studio.

Give an id to textview and align your recyclerview to below the 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. So that all your views will appear one below another.

In your xml, switch places with your TextView and the 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.

Switching them will make the TextView to be drawn after the recycler view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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