您好,登錄后才能下訂單哦!
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">ListView</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string-array name="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
<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" 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
package com.example.listview; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener{ private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView) findViewById(R.id.ListView1_1); listView.setOnItemClickListener(this); } //listView列表項的單擊事件 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub TextView textView=(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(); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。