0

我正在尝试以ListView编程方式更改文本的对齐方式,而不是更改 xml 文件。

这是我的row_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textViewRowList" 
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_width="fill_parent"
    android:textSize="20sp"
    android:paddingLeft="6dip"
    android:paddingRight="6dip" 
    android:gravity="left" 
    android:textColor="#330033"
    android:textStyle="bold"/>

并在那里定义activity_main.xml了以下内容ListView

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:dividerHeight="0dp"
    tools:listitem="@android:layout/simple_list_item_1" 
    android:background= "#FF6600">
 </ListView>

因此,mainactivity.java当用户单击我的活动上的按钮时,我试图在运行时更改列表视图中文本的对齐方式

private ListView lv;
private TextView tvRowListView;
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.row_listview, sampleStringArray));
tvRowListView = (TextView) findViewById(R.id.textViewRowList);
tvRowListView.setGravity(Gravity.CENTER);

然而,上面的最后一行是抛出空指针异常。我不确定为什么。有人可以在这里提出一些想法/解决方案。

4

1 回答 1

0

您的 textViewRowList 存在于该列表中的每个项目,如果您想更改它的属性,您需要Adapter在其中进行,getView这意味着您需要制作一个自定义适配器

于 2013-01-24T00:09:14.480 回答