简体   繁体   中英

Android xml - overlaying text/images in a relative layout

I am trying to create a background for each row of a ListActivity . I have two background images that I need to place side by side. There is a leftshopitem image, which serves as a placeholder for the item icon, and the rightshopitem image, which the textual information is to be written over.

Basically I want the following properties: I need the image icon to be centered over the leftshopitem image, and I need the textViews to be displayed over the rightshopitem image.

At the moment, I have this code - which I realise is quite wrong. The background images show up correctly aligned, but the icon and text are not. I guess my question is, how can I make an imageView a 'parent' so that I can place other objects relative to it's position.

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

You can use android_layout_toLeftOf="@id/leftbackground", android_layout_toRightOf="@id/leftbackground" and so on to "place other objects relative to it's position".

If you instead use android:align_parentLeft="true" etc, you can put one view on top of another according to your needs.

You can also set the image as backround (kind of mthe same thing as "make an imageView a 'parent'") in one of your RelativeLayouts by using android:background="@drawable/leftshopitem" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM