【发布时间】:2013-02-05 16:59:48
【问题描述】:
这是我的问题的 ListView 屏幕截图:
这是布局 XML:
<LinearLayout
android:id="@+id/viewer_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_dark"
android:orientation="vertical" >
<EditText
android:id="@+id/viewer_filter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@android:drawable/ic_menu_search"
android:hint="@string/hint_filter"
android:background="@android:color/white"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="3dp"
android:inputType="text"
android:paddingLeft="4dp"
android:selectAllOnFocus="true" >
</EditText>
<EditText
android:id="@+id/viewer_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@android:drawable/ic_menu_search"
android:hint="@string/hint_search"
android:background="@android:color/white"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="5dp"
android:inputType="text"
android:paddingLeft="4dp"
android:selectAllOnFocus="true" >
</EditText>
</LinearLayout>
<HorizontalScrollView
android:id="@+id/viewer_hscroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/viewer_top" >
<ListView
android:id="@+id/viewer_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</HorizontalScrollView>
这个场景有3个问题:
- 水平滚动视图没有覆盖整个屏幕宽度(我画了一条粗红线来标记结束)
- 水平滚动视图不水平滚动
-
ListView 行的宽度不统一(这可以通过背景色结尾看出)(详见下面的getView代码)
private static final int listRowLayout = android.R.layout.activity_list_item; private Map<String, Integer> mColors = new HashMap<String, Integer>(); @Override public View getView(int position, View convertView, ViewGroup parent) { // No logs here to keep ListView performance good ViewHolder holder; int color; if( convertView == null ) { convertView = mInflater.inflate(listRowLayout, parent, false); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(android.R.id.text1); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } String data = mData.get(position); // A compiled regex is faster than String.Contains() Matcher m = ViewHolder.regex.matcher(data); if( m.find() ) { color = mColors.get(m.group(1)); } else { color = mColors.get("V"); } holder.text.setText(data); holder.text.setBackgroundColor(color); return convertView; } private static class ViewHolder { TextView text; static Pattern regex = Pattern.compile(" ([VIDWEF])/"); }}
【问题讨论】:
-
你的 listview 宽度需要是 match_parent 而不是 wrap_content
-
@Matthew 经过测试,它确实解决了问题 #1 和 #2,问题 #3 仍然存在。请发表您的评论作为答案,如果您对问题 3 解决方案有见解,欢迎您解释。
-
奇怪的是,Eclipse 中的 lint 显示警告,对于 ListView,
android:layout_width应该是 wrap_content -
对于行,我将使用一个适配器,它允许您自定义行的样式及其高度。这是一个非常基本的示例:mkyong.com/android/android-listview-example
-
@Matthew 这次不走运。我尝试了自定义布局,但背景颜色仍然只延伸到 TextView 中的文本。
标签: android android-layout listview horizontal-scrolling