2011-11-07 83 views
0

我正在嘗試爲ListActivity的每一行創建背景。我有兩個需要並排放置的背景圖片。有一個leftshopitem圖像用作項目圖標的佔位符,以及用於寫入文本信息的rightshopitem圖像。Android xml - 在相對佈局中覆蓋文本/圖像

基本上我想要以下屬性:我需要圖像圖標居中在leftshopitem圖像上,我需要textViews顯示在rightshopitem圖像上。

目前,我有這個代碼 - 我意識到這是錯誤的。背景圖像顯示正確對齊,但圖標和文本不正確。我想我的問題是,我怎樣才能使imageView成爲'父',以便我可以放置其他對象相對於它的位置。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

     <ImageView 
      android:id="@+id/leftbackground" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:src="@drawable/leftshopitem" /> 

     <ImageView 
      android:id="@+id/img" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_centerInParent="true" 
     /> 

     <ImageView 
     android:id="@+id/rightbackground" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/leftbackground" 
     android:src="@drawable/rightshopitem" /> 


    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:paddingLeft="10dp" 
     > 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#000000" 
     /> 

    <TextView 
     android:id="@+id/weight" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#000000" 
     android:textSize="16sp" 
     /> 

    </LinearLayout> 

</RelativeLayout> 
+0

任何線框卡扣? –

回答

1

您可以使用android_layout_toLeftOf="@id/leftbackground", android_layout_toRightOf="@id/leftbackground"等以「地方其他對象相對於它的位置。」

如果您改爲使用android:align_parentLeft="true"等,您可以根據需要將一個視圖置於另一個視圖之上。

您還可以使用android:background="@drawable/leftshopitem"將圖像設置爲backround(類似於將圖像視圖設置爲父級「」)的一種RelativeLayouts。

+0

啊,我想我會用最後的辦法。我不認爲有可能使佈局區域只是圖像的大小。我剛剛意識到這是'包裝內容'意味着什麼,所以我都準備好了。非常感謝你! :) – Wozza

+0

我不得不創建一個LinearLayout,它分別包含上面的relativeLayout和linearLayout。這是因爲背景圖像正在伸展以填充通過添加正確的商品而創建的區域 – Wozza