[英]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>
一旦運行程序,它就會在編輯器中顯示一條錯誤,提示“ 錯誤:錯誤:找不到與給定名稱匹配的資源(在'layout_above'處,值為'@ id / sum_layout')。 “這是第22行在這段代碼中。
誰能告訴我這是怎么回事?
我想知道為什么您要使它變得如此復雜。
無論如何,這里是正確的代碼段
<?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>
使用@+id/sum_layout
清理項目,然后再次運行!
這是因為您在第一個引用之后聲明了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" />
嘗試這個
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" />
當您使用相對布局時,您將控件彼此關聯。當您添加任何控件時,您將相對於之前定義的控件添加下一個控件。同樣,您在此處定義了視圖分頁器布局,該布局位於id為++ id的布局下/ sum_layout。 但是您已經在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>
根據此設置布局,無論您要保持其下方還是上方。 希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.