Je travaille sur une mise en page où j'utilise un élément ListView avec des éléments de campagne RelativeLayout. Les lignes de ligne elles-mêmes ne s'affichent pas correctement.RelativeLayout d'Android semble cassé
Le problème est que le TextView txtVideoDuration est dessiné en haut de l'élément de ligne au lieu du bas. Pour cette raison le txtVideoTitle obtient une hauteur de 0. Comme vous pouvez le voir dans le XML, le txtVideoDuration est supposé être fixé au bas de ListView.
Mon but est de faire en sorte que la mise en page ressemble au tutoriel que google m'a donné. Exemple: http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
J'utilise Android 2.0.1. J'ai même branché la disposition d'exemple sans modification dans le ListView et le même comportement se produit.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imgVideoThumbnail"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:minHeight="64dip"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<TextView
android:id="@+id/txtVideoDuration"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_toRightOf="@+id/imgVideoThumbnail"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="0:00:00" />
<TextView
android:id="@+id/txtVideoTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imgVideoThumbnail"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/txtVideoDuration"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="[Title]"
android:textColor="#FFFFFF" />
</RelativeLayout>
Ce cas simple fonctionne. C'est la disposition XML racine pour l'activité. Le code buggé en haut correspond aux éléments de ligne dans la vue de liste. Il semble que le ListView lui-même le brise.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listVideoCatalog"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hello World" />
</RelativeLayout>
</merge>
Ceci est le code getView qui gonfle l'élément de campagne dans ListView.
static class ViewHolder
{
TextView txtTitle;
ImageView imgThumbnail;
TextView txtDuration;
Uri videoLocation;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.listitem_videolineitem, null);
holder = new ViewHolder();
holder.txtDuration = (TextView) convertView.findViewById(R.id.txtVideoDuration);
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtVideoTitle);
holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.imgVideoThumbnail);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
VideoFile video = this.videos.get(position);
holder.txtDuration.setText(millisToTimestamp(video.videoDuration));
holder.txtTitle.setText(video.videoTitle);
// Some debugger visual code.
holder.txtDuration.setBackgroundColor(Color.GREEN);
holder.txtTitle.setBackgroundColor(Color.CYAN);
return convertView;
}
vient d'être ajouté le code. J'utilise le pattern ViewHolder suggéré par google. –
J'ai supprimé l'orientation verticale du RelativeLayout. Ce code n'a pas contribué au bug mais merci de le signaler. Je l'ai enlevé. –
Modifier li.inflate (R.layout.listitem_videolineitem, null); à li.inflate (R.layout.listitem_videolineitem, parent, false); et voir si ça aide. – CommonsWare