android的ListView
发布时间:2016-12-15 来源:查字典编辑
摘要:strings.xmlListViewHelloworld!Settings萌萌小白张三李四王五萌萌小白张三李四王五萌萌小白张三李四王五ac...
strings.xml
< ?xmlversion="1.0"encoding="utf-8"?> < resources> < stringname="app_name">ListView< /string> < stringname="hello_world">Helloworld!< /string> < stringname="action_settings">Settings< /string> < string-arrayname="names"> < item>萌萌< /item> < item>小白< /item> < item>张三< /item> < item>李四< /item> < item>王五< /item> < item>萌萌< /item> < item>小白< /item> < item>张三< /item> < item>李四< /item> < item>王五< /item> < item>萌萌< /item> < item>小白< /item> < item>张三< /item> < item>李四< /item> < item>王五< /item> < /string-array> < /resources>
activity_main.xml
< RelativeLayoutxmlns: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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.listview.MainActivity"> < ListView android:id="@+id/ListView1_1" android:layout_width="match_parent" android:layout_height="match_parent" android:entries="@array/names" android:footerDividersEnabled="true" android:scrollbars="horizontal|vertical" android:fastScrollEnabled="true" android:listSelector="#f5ffcc" android:drawSelectorOnTop="false" > < /ListView> < /RelativeLayout> < !--1.设置滚动条 android:scrollbars="horizontal|vertical"水平和垂直方向的 android:fastScrollEnabled="true"快速滚动条 2.item之间的间隔宽度 android:dividerHeight="2dp" 3.item之间的颜色 android:divider="#00ffff" 4.选中时的颜色 android:listSelector="#00ffff" 5.选中时,将字显示出来,尤其是写了选中时的颜色时,一定要加上这一条,否则字被遮盖 android:drawSelectorOnTop="false" -->
MainActivity
packagecom.example.listview; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.Menu; importandroid.view.MenuItem; importandroid.view.View; importandroid.widget.AdapterView; importandroid.widget.TextView; importandroid.widget.AdapterView.OnItemClickListener; importandroid.widget.ListView; importandroid.widget.Toast; publicclassMainActivityextendsActivityimplementsOnItemClickListener{ privateListViewlistView; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)findViewById(R.id.ListView1_1); listView.setOnItemClickListener(this); } //listView列表项的单击事件 @Override publicvoidonItemClick(AdapterView< ?>parent,Viewview,intposition, longid){ //TODOAuto-generatedmethodstub TextViewtextView=(TextView)view; System.out.println(parent.getClass()); System.out.println("position:"+position); System.out.println("view:"+view); System.out.println(textView.getText().toString()); Toast.makeText(MainActivity.this,textView.getText().toString(),Toast.LENGTH_SHORT).show(); } }