在android中更改listview的文本大小

时间:2014-12-04 04:00:56

标签: android android-layout listview layout android-listview

我正在尝试更改listview文本大小(默认情况下它似乎相当大)并且遇到了错误。我认为这个想法是将一个textview分配给listview并更改该textview的属性,但我开始认为我的方法是错误的。如果有人不介意看我做错了什么,我将不胜感激。提前谢谢!

这是错误:

12-03 21:59:26.894  24751-24751/my.obd2connector E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: my.obd2connector, PID: 24751
    java.lang.RuntimeException: Unable to start activity ComponentInfo{my.obd2connector/my.obd2connector.MyCar}: java.lang.ClassCastException: android.widget.ArrayAdapter cannot be cast to android.widget.TextView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.widget.ArrayAdapter cannot be cast to android.widget.TextView
            at my.obd2connector.MyCar.fillList(MyCar.java:120)
            at my.obd2connector.MyCar.onStart(MyCar.java:92)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
            at android.app.Activity.performStart(Activity.java:5436)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

以下是填充listview的代码,我试图更改大小和颜色:

public void fillList() {   
        carList.add(make + " " + model + "\n" + vin);

        mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);

        mylistView.setAdapter(mylistAdapter);
        TextView tv = (TextView)mylistView.getAdapter();
        tv.setTextColor(Color.RED);
        tv.setTextSize(12);
        mylistAdapter.notifyDataSetChanged();
    }

4 个答案:

答案 0 :(得分:4)

该行:

 TextView tv = (TextView)mylistView.getAdapter();

造成了这个问题。您无法将Adapter投射到TextView。相反,您需要为ListView创建自定义布局,并在其中更改您在布局上使用的TextView的属性。

This tutorial涵盖了创建简单自定义布局的详细信息。

答案 1 :(得分:2)

只需将ListView的默认布局更改为自定义布局

即可

而不是像这样使用:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_listview_item, list);

您可以使用customLayout.xml:

adapter = new ArrayAdapter<String>(this, R.layout.customLayout, list);

customLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="20dp" />

或者您可以这样使用:

  listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations()) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView = (TextView) super.getView(position, convertView, parent);
    textView.setTextSize(12);

    return textView;
}

});

答案 2 :(得分:1)

如果要更改列表项视图大小。您可以在listitem的xml文件中执行此操作,并将其传递给适配器。

注意: TextView id应该分配android.R.id.text1

答案 3 :(得分:0)

你走在正确的道路上。

 mylistAdapter = new ArrayAdapter<String> ( this, android.R.layout, list );

也可以尝试

mylistAdapter = new ArrayAdapter<String>  (this, android.R.layout.simple_listview_item, android.R.id.listTextView , list);