android:layout_marginLeft="29dp" // 左边距
android:layout_marginTop="42dp" // 上边距
android:textSize="20sp" // 字体大小
android:text="@string/button1" 不要硬编码
控件用dp 字体用sp
android:paddingLeft="40dp" // 内左边距
android:hint="hello world" // editView中默认显示字符
List View
ListView
is a view group that displays a list of scrollable items
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView>
定义一组静态数组
<string-array name="names"> <item>张三</item> <item>李四</item> <item>王五</item> <item>傻六</item> </string-array>
android:entries="@array/names"
android:divider="#ffffaa" 列表分割的颜色
android:dividerHeight="20dp" 分割的高度
android:listSelector="#ffffee" item选中的颜色
android:fastScrollEnabled="true" 快速移动的时候 滚动条有个小方块
android:scrollbars="none" 滚动条不显示 默认是显示
android:drawSelectorOnTop="false" 颜色会在记录的后面 不会遮盖item
package com.example.android_layout_listview; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView; import android.widget.ListView; public class MainActivity extends Activity implements OnItemClickListener{ private static final String TAG = "MainActivity"; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) this.findViewById(R.id.listView1); listView.setOnItemClickListener(this); } /** * ListView 列表项的单击事件 */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.i(TAG, "parent =="+parent.getClass()); Log.i(TAG, "view =="+view.getClass()); Log.i(TAG, "position =="+position); Log.i(TAG, "id =="+id); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
08-27 09:59:44.970: I/MainActivity(7342): parent ==class android.widget.ListView 08-27 09:59:44.970: I/MainActivity(7342): view ==class android.widget.TextView 08-27 09:59:44.970: I/MainActivity(7342): position ==1 08-27 09:59:44.970: I/MainActivity(7342): id ==1
实现OnItemClickListener接口,重写onItemClick(AdapterView<?> parent, View view, int position, long id)方法:parent代表listView,view代表点击的控件,position代表位置
ListActivity
package com.example.android_listview_activity; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends ListActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] names = {"aaa1","aaa2","aaa3","aaa4","aaa5","aaa6","aaa7","aaa8"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names); // getListView(); setListAdapter(adapter); } @Override protected void onListItemClick(ListView list, View view, int position, long id) { super.onListItemClick(list, view, position, id); // 这里填充点击事件处理逻辑 } }