【问题标题】:Make a view full-width with a ListAdapter使用 ListAdapter 创建全角视图
【发布时间】:2011-01-12 20:15:05
【问题描述】:

我正在尝试在 ListView(通过 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 没有执行任何操作...

我该怎么办?

谢谢!

【问题讨论】:

    标签: android listview android-layout


    【解决方案1】:

    我遇到了类似的问题,我已按照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> 
    

    【讨论】:

      【解决方案2】:

      为什么不使用 fill_parent 来添加它们,而是使用 '0'?

      result.addView(testView, new LinearLayout.LayoutParams(FILL_PARENT ,
              LayoutParams.WRAP_CONTENT, 1));
      

      【讨论】:

      • 因为有两个视图:我希望每个视图只占总宽度的一半。我找到了一个解决方案,但现在是一个很棒的解决方案: result.setLayoutParams(new ListView.LayoutParams(totalWidth, LayoutParams.WRAP_CONTENT); 但它需要知道所需的总宽度。出于某种原因,将 LayoutParams.MATCH_PARENT 放在此处不起作用O_o
      • 是的,没有变化 :-/ 另外,我编辑了帖子以显示问题的更简单版本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2012-08-16
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多