1

我有一个 textView、editText 和 listView。我想订购 textView 和 editText 彼此相邻,因此是 LinearLayout。此外,在它们下创建一个 listView。这是 XML。

不幸的是,listView 没有出现。我确保我使用的 ArrayList 不为空。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" 
   android:gravity="center"
   android:background ="#268496" >

<LinearLayout android:id="@+id/linear"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="match_parent">

 <TextView
    android:id="@+id/prefixText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:textIsSelectable="true"
    android:textSize="12pt"
    android:typeface="sans" />

 <EditText
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/input"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:textSize="12pt"
    android:maxLength="1"
    android:typeface="sans" />

</LinearLayout>

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/linear"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="5dp"
    android:divider="#CCCCCC"
    android:dividerHeight="1dp"
    android:paddingLeft="2dp" >

   </ListView>

</RelativeLayout>

创建列表:

public void listView (){
     ListView lv;
     setContentView(R.layout.activity_main);
     lv = (ListView) findViewById(R.id.listView);
     ArrayList<String> your_array_list = new ArrayList<String>();
     your_array_list.add("foo");
     your_array_list.add("bar");
     ArrayAdapter<String> arrayAdapter =      
     new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
     lv.setAdapter(arrayAdapter); 

}
4

3 回答 3

4

您的线性布局高度为 match_parent,将其更改为 wrap_content

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" 
   android:gravity="center"
   android:background ="#268496" >

<LinearLayout android:id="@+id/linear"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

 <TextView
    android:id="@+id/prefixText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:textIsSelectable="true"
    android:textSize="12pt"
    android:typeface="sans" />

 <EditText
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/input"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:textSize="12pt"
    android:maxLength="1"
    android:typeface="sans" />

</LinearLayout>

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/linear"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="5dp"
    android:divider="#CCCCCC"
    android:dividerHeight="1dp"
    android:paddingLeft="2dp" >

   </ListView>

</RelativeLayout>
于 2013-05-24T18:28:52.910 回答
2

而不是 match_parent 供您LinearLayout 使用 wrap_content android:layout_height="wrap_content"

于 2013-05-24T18:29:00.687 回答
0

因为您LinearLayout拥有android:layout_height="match_parent",这意味着它将具有完整的高度,并且不会为 ListView 留下空间。

所以将其替换为wrap_content.

于 2013-05-24T18:32:16.367 回答