2014-01-30 58 views
0

我想知道是否有人可以解釋我遇到的一個小問題。我試圖讓我的周圍RelativeLayouts頭,有以下幾點:ImageViews和TextViews在相對佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:background="#ffffff" 
    android:layout_height="fill_parent" >   

      <ImageView 
       android:id="@+id/newsImage" 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:layout_alignParentLeft="true" 
       android:background="#ffffff" > 
      </ImageView>    

      <TextView 
       android:id="@+id/newsSubtitle" 
       android:layout_height="fill_parent" 
       android:layout_width="200dp" 
       android:textColor="#0098bf" 
       android:layout_centerVertical="true" 
       android:layout_alignRight="@+id/newsImage" 
       android:textSize="18sp"> 
      </TextView> 

      <TextView 
       android:id="@+id/newsId" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:visibility="invisible" 
       android:textSize="0sp" > 
      </TextView>  
</RelativeLayout> 

正如我在RelativeLayouts理解它,你可以對齊孩子使用「alignParentLeft =‘真’」父(RelativeLayout的),你可以使用alignRight =「@ + id/childId」將每個孩子與其他孩子對齊。對於TextViews來說這似乎工作得很好,但是我無法讓我的ImageViews正常工作 - 這就好像它們實際上似乎沒有佔用任何空間,因爲TextView只是坐在圖像的頂部,而不是對齊到圖像的權利。

我在這裏誤解了一些東西嗎?RelativeLayouts有不同的對齊圖像/文本組合的方法嗎?

回答

0

在您的textview上調用android:layout_below =「@ id/idofImageView」並將圖像添加到ImageView。

編輯:對不起,你想讓它正確的:android:align_toRightOf="@id/ImagevIEW"

+0

我已經在TextView中做了這個 'android:layout_alignRight =「@ + id/newsImage」' 我也嘗試刪除+號,但那也沒用。 –

+0

+符號表示當您要調用現有的ID時,您正在創建一個新的ID。將來不要在引用另一個視圖時使用加號。如果圖像很大,那麼textview會重疊,但代碼仍然可以工作。嘗試使用ic_launcher(應用程序圖標)作爲要檢測的圖像視圖的來源。 –

0

使用此代碼。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:background="#ffffff" 
    android:layout_height="fill_parent" >   

      <ImageView 
       android:id="@+id/newsImage" 
       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:layout_alignParentLeft="true" 
       android:background="#ffffff" > 
      </ImageView> 

      <TextView 
       android:id="@+id/newsId" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:visibility="gone" 
       android:textSize="0sp" > 
      </TextView> 

      <TextView 
       android:id="@+id/newsSubtitle" 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true" 
       android:textColor="#0098bf" 
       android:textSize="18sp" /> 

</RelativeLayout> 
+0

我試圖layout_height =「WRAP_CONTENT」和layout_height =「match_parent」,他們都給予同樣的結果 - TextView的坐在ImageView的頂部:/ –

+0

嘗試與一些類似android的android:layout_height =「100dp」或者包裝內容的dp一樣dp:layout_height =「wrap_content」' – InnocentKiller

+0

好吧,你到底想要怎樣的內容。你想要在你的圖片視圖的下方或以上顯示文本視圖。 – InnocentKiller