【问题标题】:Change text size of listview in android在android中更改listview的文本大小
【发布时间】:2015-02-01 20:42:30
【问题描述】:

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

这是错误:

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)

这是填充列表视图的代码,我试图在其中更改大小和颜色:

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();
    }

【问题讨论】:

  • 您使用的是哪个适配器?
  • 它的:public ArrayAdapter mylistAdapter;我需要这个来填充字符串的 ListView。

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


【解决方案1】:

行:

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

导致问题。您不能将 Adapter 转换为 TextView。相反,您需要为您的 ListView 创建一个自定义布局,并在其中更改您在布局上使用的 TextView 的属性。

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

【讨论】:

  • 我真希望我刚开始时能看到这个教程,哈哈,谢谢先生。
【解决方案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;
}

});

【讨论】:

  • 我试过这个...每当我制作自定义布局时,当我输入 android.R.layout 时它不会显示。它似乎只显示 simple_listview_item 和其他。是否有某个文件夹可以识别它?
  • 你可以使用第二个例子
  • 我会在我执行 setAdapter() 之后在 filllist 方法中调用它吗?
  • 你可以像这样在listview中设置适配器
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    你在正确的道路上。

     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); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-04
      • 2015-05-20
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 2013-10-26
      • 2012-09-24
      • 1970-01-01
      相关资源
      最近更新 更多