我有一个 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);
}