我正在尝试使用全宽度在ListView中创建一个View(通过ListAdapter)。 这是当前的测试代码:
LinearLayout result = new LinearLayout(context);
TextView testView = new TextView(context);
testView.setText("Aaah");
TextView test2View = new TextView(context);
test2View.setText("Eeeeh");
result.addView(testView, new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1));
result.addView(test2View, new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1));
// To make it easier to see borders
result.setBackgroundColor(Color.BLUE);
(必须放在ListAdapter的createView中)
不幸的是,两个TextView很小并且都卡在左边。 限制器实际上是结果视图本身,而不是全宽度,因此两个TextView只是共享这个小空间。
有没有人知道如何让它们占据全宽?谢谢 ! :)
代码: http://pastebin.com/6pn1hXnT
我希望TextView是全尺寸的,我将文本居中,以便我可以看到它是全宽的还是只包装内容的时候。 此代码显示左侧的文本,因此MATCH_PARENT没有执行任何操作...
我该怎么办?
谢谢!
答案 0 :(得分:4)
我遇到了类似的问题,我遵循了thread的建议。链接的主题引用对话框中的ListView,但我在弹出列表中遇到了相同的行为。为了使TextView全宽,我必须将它包装在RelativeLayout中:
<?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="wrap_content"
android:paddingLeft="14dip"
android:paddingRight="15dip"
>
<TextView
android:id="@+id/list_dialog_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
/>
</RelativeLayout>
答案 1 :(得分:0)
为什么不用fill_parent添加它们,但使用'0'?
result.addView(testView, new LinearLayout.LayoutParams(FILL_PARENT ,
LayoutParams.WRAP_CONTENT, 1));