简体   繁体   中英

Android Custom ViewGroup not rendering correctly

I have a ViewGroup that has the following xml layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/ticketBuy_viewLayout"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">
    <ImageView
        android:id="@+id/ticketBuy_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
    <TextView
        android:id="@+id/ticketBuy_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/ticketBuy_icon"/>
    <TextView
        android:id="@+id/ticketBuy_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/ticketBuy_icon"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

Later, I put these Views into a ListView. And, well, don't really get the desired result.

What I want it to be is a small rectangular view with an image to the left, a title to the top right and the date to the bottom left. I am not posting the ListView xml because it does not have anything that would be of use (just a width and height set to match_parent) However, all I get is this (This ListView has about 8 elements):

在此处输入图片说明

I think you just messed up with the layout.

Try this layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ticketBuy_viewLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <ImageView
        android:id="@+id/ticketBuy_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"

        />

    <TextView
        android:id="@+id/ticketBuy_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/ticketBuy_icon"
       />

    <TextView
        android:id="@+id/ticketBuy_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/ticketBuy_name"
      />

</RelativeLayout>

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