Search code examples
androidxmlandroid-layoutlayout

linear and relative layouts


      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
      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"
      tools:context=".MainActivity"
      android:orientation="vertical"  >

      <ImageView
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:src="@drawable/androidparty" />

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

       <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="DEALS IN:-"
       android:padding="10dp"/>

      </LinearLayout>
     
      </RelativeLayout>

      

in relative layout when i set height and weight as match_parent my text slides to the bottom of the design panel and is not visible, why does this happen?


Solution

  • You have to specify how a view is positioned within a RelativeLayout.
    You can check the doc:

    The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

    For example use android:layout_alignParentTop="true":

    <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    
           <TextView
             android:layout_alignParentTop="true"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="DEALS IN:-"
             android:padding="10dp"/>    
    
    </RelativeLayout>
    

    Also in your layout there is another issue:

    <LinearLayout
       android:orientation="vertical">
    
       <RelativeLayout
          android:layout_height="match_parent"/>
    
       <TextView/>
    
    </LinearLayout>
    

    The RelativeLayout has android:layout_height="match_parent" and in this way it fills the parent view and the TextView is outside the RelativeLayout.

    enter image description here