【问题标题】:Aligning ViewPager and TextView Vertically垂直对齐 ViewPager 和 TextView
【发布时间】:2012-07-05 16:12:17
【问题描述】:
<?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>

我一运行程序,它就会在编辑器中显示一个错误,说“错误:错误:找不到与给定名称匹配的资源(在'layout_above',值为'@id/sum_layout')。 ”,在此代码的第 22 行。

谁能告诉我怎么了?

【问题讨论】:

  • 您找到错误:找不到资源,因为它没有获得您想要在其上方设置的布局的 id。首先添加具有 id @+id/sum_layout 的布局,然后在上方添加 viewpager具有 id @+id/sum_layout 的布局。

标签: android textview android-relativelayout android-viewpager vertical-alignment


【解决方案1】:

当您使用相对布局时,您将控件相互关联。当您添加任何控件时,您添加下一个控件相对于之前定义的控件。同样在这里您定义了视图寻呼机布局,该布局位于具有 id 的布局下@+id/sum_layout。但是您已经在视图分页器下定义了布局@+id/sum_layout。所以它没有得到id@+id/sum_layout,所以它将如何在文本视图下设置视图分页器。首先添加视图,然后添加到它的低于/高于。这是你的错误:

<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>

根据此设置布局,无论您必须保持其下方或上方。 希望对你有帮助。

【讨论】:

  • 感谢您的解决方案。但这是一个实验性的。我试图通过将 ViewPager 的 layout_height 属性设置为“fill_parent”来使用它。这样做时,它占据了整个屏幕。现在好了。谢谢大家。
  • 这是我的荣幸。始终牢记您将要做什么。您与未声明的人有关 android 将如何知道向谁推荐?您正在将 textview 保持在 viewpager 以下,但首先声明 viewpager..这就是它给出错误资源未找到的原因。
【解决方案2】:

我想知道你为什么把它弄得这么复杂。

无论如何这里是纠正sn-p

<?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>

【讨论】:

    【解决方案3】:

    试试这个

    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" />
    

    【讨论】:

    • 现在,如果我将 ViewPager 的 layout_height 属性设置为 fill_parent,它将占据整个屏幕。但我希望它通过 (TotalScreenHeight - TextViewHeight) 的方式调整它的高度。我需要动态执行此操作吗?
    【解决方案4】:

    这是因为你在第一次引用之后声明了 sum_layout id。 最好的解决方案是在 /res/values/ids.xml 中声明它,如下所示:

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

    然后在你的所有布局中引用它而不用 + 关键字

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

    还有

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

    【讨论】:

    • 现在,如果我将 ViewPager 的 layout_height 属性设置为 fill_parent,它将占据整个屏幕。但我希望它通过(TotalScreenHeight - TextViewHeight)的方式调整它的高度。我需要动态执行此操作吗?
    【解决方案5】:

    使用@+id/sum_layout

    清理项目并再次运行!

    【讨论】:

    • 它工作正常,但出现了一个新问题。我的 TextView 中曾经有一些文本文本。现在它没有显示。
    • 现在,如果我将 ViewPager 的 layout_height 属性设置为 fill_parent,它将占据整个屏幕。但我希望它通过(TotalScreenHeight - TextViewHeight)的方式调整它的高度。我需要动态执行此操作吗?
    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    相关资源
    最近更新 更多