標籤:android
基於ArrayAdapter 建立ListView
程式清單
listview2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list1" android:divider="#0f0" android:dividerHeight="3px" android:footerDividersEnabled="false" /></LinearLayout>
MainActivity.java
package xiaocool.net.listviewtest;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends ActionBarActivity { private ListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview2); list=(ListView)this.findViewById(R.id.list1); //定義一個數組 String arr[]={"校酷網路1","校酷網路2","校酷網路3","校酷網路4","校酷網路5"}; //將數組封裝成ArrayAdapter ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.array_item,R.id.textView,arr); list.setAdapter(adapter); }}
array_item..xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textView" android:textSize="20sp" android:textColor="#f0f" android:padding="20px" /></LinearLayout>
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5B/43/wKiom1UDkHeBUIooAAFYZ2kk_-k792.jpg" title="QQ20150314093422.png" alt="wKiom1UDkHeBUIooAAFYZ2kk_-k792.jpg" />
Android 學習--ListView 的使用(二)