【问题标题】:Setting text for a TextView in ListActivity在 ListActivity 中为 TextView 设置文本
【发布时间】:2013-05-11 21:16:14
【问题描述】:

我通过扩展 ListActivity 来填充列表。我在列表中添加了一个标题以显示列表中的项目总数。

我在单独的布局文件 header.xml 中维护了标题视图,如下所示。

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView 
        android:id="@+id/headerTextView"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="Total number of devices :" 
        android:textSize="15dip"
        android:layout_weight="1" />
</LinearLayout>

这就是我在 ListViewActivity 类的列表中添加标题的方式,

ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);

如何设置标题 TextView 的文本?我尝试了以下方法,它为 TextView 返回了 null。

TextView textBox = (TextView)findViewById(R.id.);
  textBox.setText("Count");

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    试试这个

    View header = (View)inflater.inflate(R.layout.header, lv, false);
    lv.addHeaderView(header, null, false);
    

    在标题中获取您的 TextView

    TextView textBox = (TextView)header.findViewById(R.id.headerTextView);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-23
      • 2013-10-27
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2020-03-01
      • 2012-09-07
      相关资源
      最近更新 更多