简体   繁体   中英

Aligning ViewPager and TextView Vertically

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout-Normal-Portrait -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/thumb_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <android.support.v4.view.ViewPager
            android:id="@+id/mypager"
            android:layout_width="fill_parent"
            android:layout_height="300dp"
            android:layout_above="@id/sum_layout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" >
        </android.support.v4.view.ViewPager>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/sum_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@id/thumb_layout" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="6dp"
            android:text="@string/lorem__ipsum"
            android:textColor="@color/black" />
    </RelativeLayout>

</RelativeLayout>

As soon as I run the program, it shows a error in the editor saying " error: Error: No resource found that matches the given name (at 'layout_above' with value '@id/sum_layout'). " which is at line 22 in this code.

Can any one tell, what is wrong?

I wonder why you are making it so complex.

Anyways here is corrected snippet

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout-Normal-Portrait -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" > <---No need to specify android:orientation

    <android.support.v4.view.ViewPager
        android:id="@+id/mypager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/textView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="6dp"
        android:text="Hello"
        android:textColor="@android:color/black" />  <---Place your text String here 

</RelativeLayout>

use @+id/sum_layout

and clean Project and run again!

This is because you declare sum_layout id AFTER its first reference. The best solution is to declare it in the /res/values/ids.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<resources> 
     <item type="id" name="sum_layout" />
</resources>

And then reference it in all your layouts without the + keyword

 <android.support.v4.view.ViewPager
        android:layout_above="@id/sum_layout"
    />

And

 <RelativeLayout android:id="@id/sum_layout" />

Try This

xmlns:tools="http://schemas.android.com/tools"


android:layout_width="fill_parent"


android:layout_height="fill_parent"

android:background="@color/white"

android:orientation="vertical" >

<android.support.v4.view.ViewPager

    android:id="@+id/mypager"

    android:layout_width="fill_parent"

    android:layout_height="300dp"

    android:layout_alignParentLeft="true"

    android:layout_alignParentRight="true"

    android:layout_alignParentTop="true" >

</android.support.v4.view.ViewPager>

<TextView
    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:padding="6dp"

    android:textColor="@color/black" />

When you are using relative layout, you are relating the controls with each other.when you add any control you add next control with respect to the control defined before.Likewise here you have defined view pager layout which is under layout having id @+id/sum_layout. but you have defined layout @+id/sum_layout under view pager.so it doesn't get id @+id/sum_layout,so how it will set view pager under the text view.first of all add the view and then add to its under/above.Here is your mistake:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.ViewPager
            android:id="@+id/mypager"
            android:layout_width="fill_parent"
            android:layout_height="300dp">    
    </android.support.v4.view.ViewPager>

    <TextView 
        android:id="@+id/mytext"
        android:text="@string/lorem__ipsum"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mypager"/>
</RelativeLayout>

Set the layout according to this wherever you have to keep to its below or above. hope it will help 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