基于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>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。