简体   繁体   中英

how to make a relative layout under Scroll view in android studio

i have a layout that have 3 text view and one scroll view and one relative layout with three buttons, now I want when I scrolling then my second relative layout come with scroll view, but that relative layout all the times comes under the last text view, because I gave layout below because I tried every way but I don't get successfully. this is my code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/scrl"
    android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#39dad9d9"
android:padding="5dp"
    android:orientation="vertical"
    tools:context=".Bekhon">



    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20dp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:textColor="@color/light_font"
        android:shadowColor="@color/text_shadow"
        android:shadowDx="10"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:id="@+id/ttl"
  android:text=""
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="15dp"
        android:textStyle="normal"
        android:gravity="top|right"
        android:id="@+id/dsc"
        android:textDirection="rtl"
        android:elegantTextHeight="true"
        android:layout_below="@id/ttl"
        android:background="@drawable/bt"
        android:lineSpacingExtra="10dp"
        android:layout_marginTop="10dp"
      android:text=""
        android:layout_centerHorizontal="true" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15dp"
    android:textStyle="italic"
    android:textDirection="anyRtl"
    android:layout_marginTop="5dp"
    android:id="@+id/nevis"
    android:layout_below="@+id/dsc"
    android:layout_alignParentRight="true"
    android:textColor="#a19e9e"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
android:layout_below="@+id/nevis"
        android:background="#3699db"
        android:gravity="center"
     >

        <Button
            android:id="@+id/begam"
            android:layout_width="50dp"
            android:layout_height="60dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:background="@mipmap/wings" />

        <Button
            android:id="@+id/like"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:background="@mipmap/khli" />

        <TextView
            android:id="@+id/likes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/like"
            android:layout_centerHorizontal="true"
            android:text=""
            android:textColor="#fff"
            android:textSize="16dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/nxt"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="@mipmap/arr" />

    </RelativeLayout>


</RelativeLayout>

    </ScrollView>

Your first RelativeLayout's height is set to match_parent . This way it will never scroll, because to have something to scroll, the ScrollView child has to be bigger than the ScrollView itself . Try changing both of your RelativeLayout's height to wrap_content and see if it works.

Let me know.

The scroll view works based on its children .

Here under the scroll view you are having a relative layout 1 children and under that relative layout 1 you are having another child relative layout 2

The problem is that the child relative layout 1 occupies all the space in your xml as you used

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

match parent for width and height

in this case you are using scroll view , so height is basically needed for scroll view

change :

android:layout_height="match_parent"

to :

android:layout_height="wrap_parent"

or you can specify height value in dp , but use correct value length inorder to display both relative layouts.

change the height of your first relative layout to get this work correctly .

(Note: it is not related to your question

i have noticed you are using dp in your text view for size change that to sp )

i hope my answer helps you.

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