简体   繁体   English

垂直对齐ViewPager和TextView

[英]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. 一旦运行程序,它就会在编辑器中显示一条错误,提示“ 错误:错误:找不到与给定名称匹配的资源(在'layout_above'处,值为'@ id / sum_layout')。 “这是第22行在这段代码中。

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 使用@+id/sum_layout

and clean Project and run again! 清理项目,然后再次运行!

This is because you declare sum_layout id AFTER its first reference. 这是因为您在第一个引用之后声明了sum_layout id。 The best solution is to declare it in the /res/values/ids.xml like this: 最好的解决方案是在/res/values/ids.xml中声明它,如下所示:

<?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. 当您使用相对布局时,您将控件彼此关联。当您添加任何控件时,您将相对于之前定义的控件添加下一个控件。同样,您在此处定义了视图分页器布局,该布局位于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: 但是您已经在view pager下定义了布局@ + id / sum_layout,因此它不会获得id @ + id / sum_layout,因此它将如何在文本view下设置view pager。首先添加视图,然后添加到其视图中下方/上方。这是您的错误:

<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. 希望对您有帮助。

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

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