如何让 Android ListView item 的背景颜色一直延伸到最后?
How to get background color for Android ListView item to extend all the way to the end?
我试图强制我的应用程序中某些 ListView 项目的背景颜色与所有其他项目不同。 Stack Overflow 和其他地方的示例非常简单。但由于某种原因,背景颜色仅在顶部 TextView 的宽度范围内发生变化。请注意,在所附图片中,每行有两个 TextView 项目。
我决定记录列表视图项的宽度,奇怪的是它的宽度为 0。当然出乎意料,但也许在记录(执行 getView)时它还没有被扩展到他们的全部。
我在 getView() 调用中设置背景颜色,所以它的代码如下。该项目的 xml 低于该值。
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
if(arg1 == null)
{
LayoutInflater inflater = (LayoutInflater) m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem_assessments, arg2, false);
}
TextView title = (TextView)arg1.findViewById(R.id.tvTitle);
TextView date = (TextView)arg1.findViewById(R.id.tvDate);
if (arg0 <= m_list.size())
{
Attrs attrs = m_list.get(arg0);
title.setText(attrs.m_sTitle);
date.setText(attrs.m_sDetail);
if (Character.isDigit(attrs.m_sActivationCount.charAt(0)))
{
if (!attrs.m_sActivationCount.equals("0"))
{
Log.i("Adapter", " Width of list item view is: " + arg1.getWidth());
arg1.setBackgroundColor(Color.YELLOW);
}
else
Log.i("Adapter", "Width of list item view is: " + arg1.getWidth());
}
else
arg1.setTag("");
return arg1;
}
这是 ListView 项目的 xml 资源:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="23dp"
android:layout_marginTop="10dp"
android:text="Assessment title"
android:textStyle="bold"
android:textSize="16sp" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle"
android:layout_alignLeft="@+id/tvTitle"
android:background="@color/LightBlue"
android:text="date or author" />
</RelativeLayout>
请注意,我使用 match_parent 作为列表项的宽度,因此它似乎会完全伸展,当然不会是 0。也不是因为它不是 wrap_content 它应该只延伸到最远作为最宽的 TextView。
为了清楚起见,我需要在我的测试中将背景颜色黄色一直延伸到右侧。非常感谢任何帮助。
更新
通过将代码移动到 OnItemClickListener 事件中,我已经能够记录一些准确的宽度信息。整个ListView的宽度是1312,但是ListView的item的宽度确实太短了,而且随着黄色背景的显示而变化。第一项是 696。现在很清楚布局没有拉伸以匹配父项。如何强制它这样做?它的表现好像我使用 match_content 而不是 match_parent 作为 layout_width。现在尝试在 运行 时间(在膨胀时间)验证 layout_width.
问题在于您的 TextView 的宽度,因为它们设置为 wrap_content。
将它们更改为
android:match_parent
我试图强制我的应用程序中某些 ListView 项目的背景颜色与所有其他项目不同。 Stack Overflow 和其他地方的示例非常简单。但由于某种原因,背景颜色仅在顶部 TextView 的宽度范围内发生变化。请注意,在所附图片中,每行有两个 TextView 项目。
我决定记录列表视图项的宽度,奇怪的是它的宽度为 0。当然出乎意料,但也许在记录(执行 getView)时它还没有被扩展到他们的全部。
我在 getView() 调用中设置背景颜色,所以它的代码如下。该项目的 xml 低于该值。
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
if(arg1 == null)
{
LayoutInflater inflater = (LayoutInflater) m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem_assessments, arg2, false);
}
TextView title = (TextView)arg1.findViewById(R.id.tvTitle);
TextView date = (TextView)arg1.findViewById(R.id.tvDate);
if (arg0 <= m_list.size())
{
Attrs attrs = m_list.get(arg0);
title.setText(attrs.m_sTitle);
date.setText(attrs.m_sDetail);
if (Character.isDigit(attrs.m_sActivationCount.charAt(0)))
{
if (!attrs.m_sActivationCount.equals("0"))
{
Log.i("Adapter", " Width of list item view is: " + arg1.getWidth());
arg1.setBackgroundColor(Color.YELLOW);
}
else
Log.i("Adapter", "Width of list item view is: " + arg1.getWidth());
}
else
arg1.setTag("");
return arg1;
}
这是 ListView 项目的 xml 资源:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="23dp"
android:layout_marginTop="10dp"
android:text="Assessment title"
android:textStyle="bold"
android:textSize="16sp" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle"
android:layout_alignLeft="@+id/tvTitle"
android:background="@color/LightBlue"
android:text="date or author" />
</RelativeLayout>
请注意,我使用 match_parent 作为列表项的宽度,因此它似乎会完全伸展,当然不会是 0。也不是因为它不是 wrap_content 它应该只延伸到最远作为最宽的 TextView。
为了清楚起见,我需要在我的测试中将背景颜色黄色一直延伸到右侧。非常感谢任何帮助。
更新
通过将代码移动到 OnItemClickListener 事件中,我已经能够记录一些准确的宽度信息。整个ListView的宽度是1312,但是ListView的item的宽度确实太短了,而且随着黄色背景的显示而变化。第一项是 696。现在很清楚布局没有拉伸以匹配父项。如何强制它这样做?它的表现好像我使用 match_content 而不是 match_parent 作为 layout_width。现在尝试在 运行 时间(在膨胀时间)验证 layout_width.
问题在于您的 TextView 的宽度,因为它们设置为 wrap_content。 将它们更改为
android:match_parent