Android的RelativeLayout的似乎打破Android、RelativeLayout

2023-09-05 02:18:23 作者:折花载酒少年事.

我工作的一个布局,我使用带有RelativeLayout的行项目的ListView。该lineItems的本身无法正确显示。

的问题是,txtVideoDuration TextView的绘制在订单项而不是底部的顶部。这是因为txtVideoTitle得到一个0高度,你可以在XML看到txtVideoDuration应该是夹到ListView的底部。

我的目标是使布局类似,谷歌给我的教程。例如:http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

我使用的是Android 2.0.1。我甚至插在例如布局进行改造成ListView和相同的行为发生。

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =机器人:ATTR /列表preferredItemHeight
机器人:填充=6dip>
< ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:ID =@ + ID / imgVideoThumbnail
机器人:layout_alignParentTop =真
机器人:layout_alignParentBottom =真
机器人:=了minHeight64dip
机器人:layout_marginRight =6dip
机器人:SRC =@可绘制/图标/>
<的TextView
机器人:ID =@ + ID / txtVideoDuration
机器人:layout_width =FILL_PARENT
    机器人:layout_height =26dip
    机器人:layout_toRightOf =@ + ID / imgVideoThumbnail
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentRight =真
    机器人:单线=真
    机器人:ellipsize =金字招牌
机器人:文本=0:00:00/>
<的TextView
机器人:ID =@ + ID / txtVideoTitle
机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_toRightOf =@ + ID / imgVideoThumbnail
    机器人:layout_alignParentRight =真
    机器人:layout_alignParentTop =真
    机器人:layout_above =@ + ID / txtVideoDuration
    机器人:layout_alignWithParentIfMissing =真
    机器人:重力=center_vertical
机器人:文本=[标题]
机器人:文字颜色=#FFFFFF/>

< / RelativeLayout的>
 

这个简单的情况下工作。这是为活动的根XML布局。马车code上方是在列表视图中的行项目。看来,ListView控件本身是打破了。

 < XML版本=1.0编码=UTF-8&GT?;
<合并的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>

<的ListView
机器人:ID =@ + ID / listVideoCatalog
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>
<的TextView
机器人:ID =@ + ID / txtName的
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:文本=Hello World的/>
< / RelativeLayout的>
< /合并>
 

这是getView code行项膨胀到ListView中。

 静态类ViewHolder
{
TextView的txtTitle;
ImageView的imgThumbnail;
TextView的txtDuration;
乌里videoLocation;
}

@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
ViewHolder持有人;

如果(convertView == NULL)
{
LayoutInflater力=(LayoutInflater)this.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
convertView = li.inflate(R.layout.listitem_videolineitem,NULL);
持有人=新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(保持器);
}
	其他
{
支架=(ViewHolder)convertView.getTag();
}

VideoFile视频= this.videos.get(位置);
holder.txtDuration.setText(millisToTimestamp(video.videoDuration));
holder.txtTitle.setText(video.videoTitle);

            //一些调试可视code。
holder.txtDuration.setBackgroundColor(Color.GREEN);
holder.txtTitle.setBackgroundColor(Color.CYAN);


返回convertView;
}
 
Android格局一 RelativeLayout

解决方案

你是如何夸大你行的观点是越野车?你能提供一个code段?

此外,机器人:方向=垂直不适用于 RelativeLayout的

I'm working on a layout where I use a ListView with RelativeLayout line items. The lineitems themselves are not displaying correctly.

The issue is that the txtVideoDuration TextView is drawn at the top of the line item instead of the bottom. Because of this the txtVideoTitle gets a height of 0. As you can see in the XML the txtVideoDuration is supposed to be clamped to the bottom of the ListView.

My goal is to have the layout resemble the tutorial that google gave me. Example: http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

I'm using Android 2.0.1. I've even plugged in the example layout with out modification into the ListView and the same behavior happens.

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

This simple case works. This is the root XML layout for the activity. The buggy code at the top is the line items in the list view. It seems that the ListView itself is breaking it.

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

This is the getView Code that inflates the line item into the 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;
}

解决方案

How are you inflating your row views that are "buggy"? Can you provide a code snippet?

Also, android:orientation="vertical" is not valid for RelativeLayout.