我试图做一个非常简单的事情(显示一个填充的ListView),但我无法做到。我的代码无法正常工作,我找不到问题,所以我希望其他人可以帮助我:)

我的XML定义了listView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10px">

<TextView
    android:id="@+id/descripcion"
    android:text="@string/descripcion"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"    />

<ListView
    android:id="@+id/listaConfig"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"    />

</LinearLayout>

活动:

public class s_config extends Activity {

    public ArrayAdapter<String> lvAdapter;
    public ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.s_config); 

      final String[] datos = new String[]{"Elem1","Elem2","Elem3","Elem4","Elem5"};

      lvAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,datos);
      lv = (ListView) findViewById(R.id.listaConfig);
      lv.setAdapter(lvAdapter);

    }
}

当我启动该应用程序时,它可以很好地显示XML的文本视图,但没有ListView的迹象...我是否缺少什么?

有帮助吗?

解决方案

在您的XML布局中有一个问题:

将布局定向设置为垂直 android:orientation="vertical" 这使您的列表视图可见。

笔记:

如果不设置方向,它的值将是 水平为默认值.

编辑布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10px"
   android:orientation="vertical">

<TextView
    android:id="@+id/descripcion"
    android:text="@string/descripcion"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"    />

<ListView
    android:id="@+id/listaConfig"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"    />

</LinearLayout>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top