[英]RelativeLayout children not aligning to a centered ImageView
有點尷尬,但我在對齊相對布局中的某些視圖時遇到了一些基本問題。 我的 XML 如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MyActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/background"
android:scaleType="centerCrop"
android:src="@drawable/background"/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:id="@+id/imageview"
android:src="@drawable/text" />
<Button
android:layout_width="200dp"
android:layout_height="60dp"
android:enabled="false"
android:id="@+id/button"
android:background="@drawable/buttonxml"
android:layout_below="@+id/imageview"
android:layout_alignLeft="@id/imageview"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/coin"
android:src="@drawable/coin"
android:layout_below="@id/imageview"
android:layout_alignRight="@id/imageview"/>
</RelativeLayout>
我有一個居中的 ImageView (id/imageview),我想要兩個視圖對齊在下方 - 一個在左邊,一個在右邊。 但是,對於上面的 xml,似乎兩個較小的視圖都與 id/imageview 對齊,而沒有考慮到它已居中的事實:
在我將 id/imageview 居中之前,它看起來如下所示(請注意,較小的視圖現在按預期對齊,盡管它們沒有移動):
除了使用 LinearLayouts 的集合之外,還有其他想法嗎? 我覺得這是一個足夠簡單的案例,可以用 RelativeLayout 來處理,我可能只是在做一些愚蠢的事情。 提前致謝!
您是否有理由希望您的 RelativeLayout layout_width
和 height 來包裝內容? 將它們設置為match_parent
可以幫助您進行中心對齊。
順便說一下,您的Button
設置android:layout_below="@+id/imageview"
而不是android:layout_below="@id/imageview"
。
將 align_left 替換為 to_left 並將 align_right 替換為 to_right
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.