9.用户接口UI布局----View控件的概述之ViewGroup
2012-07-29 20:34
483 查看
1.LinearLayout 线性布局(主要分为水平和垂直线性布局)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
[b]fill_parent会使得android:layout_weight="1" 分配的比重均衡[/b]
[b]android:gravity 表示子控件对齐方式或是内容文本的对齐方式[/b]
[b]// [b]horizontal表示垂直线性布局[/b][/b]
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"
android:text="@string/hello" />
<TextView
android:id="@+id/tv1"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00FF00"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/textView1"
android:textSize="20sp" />
</LinearLayout>
[b][b]//[/b][/b][b][b] [b]vertical[/b][/b][b][b][b]表示水平线性布局[/b][/b][/b][/b]
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<EditText
android:id="@+id/edtxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shap"
android:drawableLeft="@drawable/title"
android:hint="@string/hello"
android:inputType="phone" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="点偶" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="点偶222" />
<Button
android:id="@+id/callbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="点偶打电话" />
<Button
android:id="@+id/smsbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="点偶发短信" />
</LinearLayout>
2.AbsoluteLayout 绝对布局
[b][b][b]android:layout_x="1px"
android:layout_y="30px"[/b][/b][/b]
[b][b][b]用于定位[/b][/b][/b]
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clayout_weight="1">
<AutoCompleteTextView
android:id="@+id/hello11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="1px"
android:layout_y="30px"
android:text="@string/helloSecond" />
</AbsoluteLayout>
3.FrameLayout框架布局(加入的控件可以层叠)
android:layout_gravity 用于定位
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="@string/helloSecond" />
<EditText
android:id="@+id/helloSecondet"
android:hint="@string/helloSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_gravity="center"
android:drawableLeft="@drawable/title"
android:imeActionLabel="wwwwwwww"
/>
<Button
android:id="@+id/rtnbtn"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点偶返回" />
</FrameLayout>
4.RelativeLayout相对布局
子控件位置相关:android:layout_toRightOf
[b]子控件距离相关:android:layout_margin="10dp"
子控件与父控件位置相关:android:layout_alignParentBottom[/b]
[b]对齐相关:android:layout_alignBaseline[/b]
<RelativeLayout >
<TextView android:id="@+id/tt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ThirdActivity6"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DatePickerDialog"
android:layout_toRightOf="@id/tt1"
android:onClick="showDatePickerDialog" android:layout_alignBaseline="@id/tt1"/>
</RelativeLayout>
5.TableLayout 表格布局
隐藏指定列用逗号隔开:android:collapseColumns
[b]扩张指定列(留有空白的话):android:stretchColumns[/b]
[b][b][b][b]收缩指定列(换行)适应屏幕:[/b]android:shrinkColumns[/b][/b][/b]
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:collapseColumns="3"
android:stretchColumns="1"
android:shrinkColumns="2"
>
<TableRow >
<DatePicker
android:id="@+id/dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ThirdActivity2"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ThirdActivity3"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ThirdActivity4"
/></TableRow>
6.ListView (is a view group that displays a list of scrollable items.)
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsListView
android.widget.ListView
6.1 ArrayAdapter
// layout/list.xml
[b]android:list 名字不能改变 [/b]
android:empty [b][b]名字不能改变 传入的数组或list为空数据时显示,注意不能传入null[/b][/b]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编号"
android:background="#0000FF" />
</LinearLayout>
// Myactivity (extends ListActivity)
可以不需要setContentView(),则使用默认的layout
android:scrollingCache="false" 拖动背景改变去使能
android:divider="" 间隔
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
// Get the string array
String[] countries = getResources().getStringArray(R.array.planets_array);
ListAdapter adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,countries);
setListAdapter(adapter);
Log.i(TAG, "ThirdActivity-->onCreate");
}
// values/strings.xml
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
6.2 SimpleAdapter
[b]// layout/copy.xml[/b]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编号"
android:background="#0000FF"
android:layout_weight="1"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:background="#0000FF"
android:layout_weight="1"/>
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
android:background="#0000FF"
android:layout_weight="1"/>
</LinearLayout>
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" ></ListView>
</LinearLayout>
[b]// layout/[b]list_item.xml[/b][/b]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编号"
android:background="#0000FF"
android:layout_weight="1"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:background="#0000FF"
android:layout_weight="1" />
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
android:background="#0000FF"
android:layout_weight="1 " />
</LinearLayout>
[b]// Myactivity (extends ListActivity) [/b]
setContentView(R.layout.copy);
ListAdapter adapter1=new SimpleAdapter(this,getData(),R.layout.list_item,
new String[]{"id","name","gender"},
new int[]{R.id.id,R.id.name,R.id.gender});
// setListAdapter(adapter);
setListAdapter(adapter1);
(
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "G1");
map.put("name", "google 1");
map.put("gender", "男");
list.add(map);
map = new HashMap<String, Object>();
map.put("id", "G2");
map.put("name", "google 2");
map.put("gender", "女");
list.add(map);
return list;
}
)
6.3 SimpleCursorAdapter
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
ListAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.list_item,
cursor, new String[]{People._ID,People.NAME,People.NUMBER},
new int[]{R.id.id,R.id.name,R.id.gender});
//setListAdapter(adapter1);
setListAdapter(listAdapter);
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。
startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。
需要在AndroidManifest.xml中如权限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
7 GridView
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsListView
android.widget.AbsListViewandroid.widget.GridView
// layout/grid.xml
<?xml version="1.0" encoding="UTF-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
// ayout/grid_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/id"
android:layout_width="100dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:layout_gravity="center"
android:padding="4dp"
android:background="#0000FF" />
<TextView
android:id="@+id/tite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="4dp"
android:background="#0000FF"/>
</LinearLayout>
// MyActivity.java
onCreate片段
setContentView(R.layout.grid);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, getData()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(GridViewDemoActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
list方法
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G0");
map.put("imageid", R.drawable.sample_0);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("imageid", R.drawable.sample_1);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G2");
map.put("imageid", R.drawable.sample_2);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G3");
map.put("imageid", R.drawable.sample_3);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G4");
map.put("imageid", R.drawable.sample_4);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G5");
map.put("imageid", R.drawable.sample_5);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G6");
map.put("imageid", R.drawable.sample_6);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G7");
map.put("imageid", R.drawable.sample_7);
list.add(map
return list;
}
内部类三部曲
android:layout_width="fill_parent"
android:layout_height="fill_parent"
[b]fill_parent会使得android:layout_weight="1" 分配的比重均衡[/b]
[b]android:gravity 表示子控件对齐方式或是内容文本的对齐方式[/b]
[b]// [b]horizontal表示垂直线性布局[/b][/b]
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"
android:text="@string/hello" />
<TextView
android:id="@+id/tv1"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00FF00"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/textView1"
android:textSize="20sp" />
</LinearLayout>
[b][b]//[/b][/b][b][b] [b]vertical[/b][/b][b][b][b]表示水平线性布局[/b][/b][/b][/b]
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<EditText
android:id="@+id/edtxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shap"
android:drawableLeft="@drawable/title"
android:hint="@string/hello"
android:inputType="phone" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="点偶" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="点偶222" />
<Button
android:id="@+id/callbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="点偶打电话" />
<Button
android:id="@+id/smsbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF0000"
android:text="点偶发短信" />
</LinearLayout>
2.AbsoluteLayout 绝对布局
[b][b][b]android:layout_x="1px"
android:layout_y="30px"[/b][/b][/b]
[b][b][b]用于定位[/b][/b][/b]
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clayout_weight="1">
<AutoCompleteTextView
android:id="@+id/hello11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="1px"
android:layout_y="30px"
android:text="@string/helloSecond" />
</AbsoluteLayout>
3.FrameLayout框架布局(加入的控件可以层叠)
android:layout_gravity 用于定位
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="@string/helloSecond" />
<EditText
android:id="@+id/helloSecondet"
android:hint="@string/helloSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_gravity="center"
android:drawableLeft="@drawable/title"
android:imeActionLabel="wwwwwwww"
/>
<Button
android:id="@+id/rtnbtn"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点偶返回" />
</FrameLayout>
4.RelativeLayout相对布局
子控件位置相关:android:layout_toRightOf
[b]子控件距离相关:android:layout_margin="10dp"
子控件与父控件位置相关:android:layout_alignParentBottom[/b]
[b]对齐相关:android:layout_alignBaseline[/b]
<RelativeLayout >
<TextView android:id="@+id/tt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ThirdActivity6"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DatePickerDialog"
android:layout_toRightOf="@id/tt1"
android:onClick="showDatePickerDialog" android:layout_alignBaseline="@id/tt1"/>
</RelativeLayout>
5.TableLayout 表格布局
隐藏指定列用逗号隔开:android:collapseColumns
[b]扩张指定列(留有空白的话):android:stretchColumns[/b]
[b][b][b][b]收缩指定列(换行)适应屏幕:[/b]android:shrinkColumns[/b][/b][/b]
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:collapseColumns="3"
android:stretchColumns="1"
android:shrinkColumns="2"
>
<TableRow >
<DatePicker
android:id="@+id/dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ThirdActivity2"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ThirdActivity3"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ThirdActivity4"
/></TableRow>
6.ListView (is a view group that displays a list of scrollable items.)
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsListView
android.widget.ListView
6.1 ArrayAdapter
// layout/list.xml
[b]android:list 名字不能改变 [/b]
android:empty [b][b]名字不能改变 传入的数组或list为空数据时显示,注意不能传入null[/b][/b]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编号"
android:background="#0000FF" />
</LinearLayout>
// Myactivity (extends ListActivity)
可以不需要setContentView(),则使用默认的layout
android:scrollingCache="false" 拖动背景改变去使能
android:divider="" 间隔
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
// Get the string array
String[] countries = getResources().getStringArray(R.array.planets_array);
ListAdapter adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,countries);
setListAdapter(adapter);
Log.i(TAG, "ThirdActivity-->onCreate");
}
// values/strings.xml
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
6.2 SimpleAdapter
[b]// layout/copy.xml[/b]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编号"
android:background="#0000FF"
android:layout_weight="1"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:background="#0000FF"
android:layout_weight="1"/>
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
android:background="#0000FF"
android:layout_weight="1"/>
</LinearLayout>
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" ></ListView>
</LinearLayout>
[b]// layout/[b]list_item.xml[/b][/b]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编号"
android:background="#0000FF"
android:layout_weight="1"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:background="#0000FF"
android:layout_weight="1" />
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
android:background="#0000FF"
android:layout_weight="1 " />
</LinearLayout>
[b]// Myactivity (extends ListActivity) [/b]
setContentView(R.layout.copy);
ListAdapter adapter1=new SimpleAdapter(this,getData(),R.layout.list_item,
new String[]{"id","name","gender"},
new int[]{R.id.id,R.id.name,R.id.gender});
// setListAdapter(adapter);
setListAdapter(adapter1);
(
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "G1");
map.put("name", "google 1");
map.put("gender", "男");
list.add(map);
map = new HashMap<String, Object>();
map.put("id", "G2");
map.put("name", "google 2");
map.put("gender", "女");
list.add(map);
return list;
}
)
6.3 SimpleCursorAdapter
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
ListAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.list_item,
cursor, new String[]{People._ID,People.NAME,People.NUMBER},
new int[]{R.id.id,R.id.name,R.id.gender});
//setListAdapter(adapter1);
setListAdapter(listAdapter);
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。
startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。
需要在AndroidManifest.xml中如权限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
7 GridView
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsListView
android.widget.AbsListViewandroid.widget.GridView
// layout/grid.xml
<?xml version="1.0" encoding="UTF-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
// ayout/grid_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/id"
android:layout_width="100dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:layout_gravity="center"
android:padding="4dp"
android:background="#0000FF" />
<TextView
android:id="@+id/tite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="4dp"
android:background="#0000FF"/>
</LinearLayout>
// MyActivity.java
onCreate片段
setContentView(R.layout.grid);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, getData()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(GridViewDemoActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
list方法
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G0");
map.put("imageid", R.drawable.sample_0);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("imageid", R.drawable.sample_1);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G2");
map.put("imageid", R.drawable.sample_2);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G3");
map.put("imageid", R.drawable.sample_3);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G4");
map.put("imageid", R.drawable.sample_4);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G5");
map.put("imageid", R.drawable.sample_5);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G6");
map.put("imageid", R.drawable.sample_6);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G7");
map.put("imageid", R.drawable.sample_7);
list.add(map
return list;
}
内部类三部曲
class ImageAdapter extends BaseAdapter{ private Context context; private List<Image> images; private LayoutInflater inflater; public ImageAdapter(Context context, List<Map<String, Object>> list) { super(); this.context=context; images=getImages(list); inflater = LayoutInflater.from(context); } private List<Image> getImages(List<Map<String, Object>> list) { List<Image> images=new ArrayList<Image>(); for(Map<String, Object> map:list){ images.add(new Image(map.get("title").toString(), Integer.parseInt(map.get("imageid").toString()))); } return images; } public int getCount() { return images.size(); } public Image getItem(int position) { // TODO Auto-generated method stub return images.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = new ViewHolder(); if (convertView==null) { convertView = inflater.inflate(R.layout.grid_item, null); viewHolder.title=(TextView)convertView.findViewById(R.id.tite); viewHolder.image=(ImageView)convertView.findViewById(R.id.id); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder)convertView.getTag(); } viewHolder.title.setText(images.get(position).getTitle()); viewHolder.image.setImageResource(images.get(position).getImageId()); return convertView; } }
class Image{ private String title; private int imageId; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getImageId() { return imageId; } public void setImageId(int imageid) { this.imageId = imageid; } public Image(String title,int imageid){ this.title=title; this.imageId=imageid; } }
class ViewHolder{ TextView title; ImageView image; }
相关文章推荐
- 4.用户接口UI布局----View控件的概述之EditText
- 7.用户接口UI布局----View控件的概述之Spinner
- 6.用户接口UI布局----View控件的概述之RadioButton和CheckBox
- 5.用户接口UI布局----View控件的概述之Button
- 8.用户接口UI布局----View控件的概述之Pickers及pickers的Dialog控件
- 组合View-标题栏自绘控件圆形进度条/扫描二维码/自定义ViewGroup阶梯布局
- iOS开发之UI控件——UIPickerView实现点菜系统界面布局
- 自己定义ViewGroup控件(二)----->流式布局进阶(二)
- 自定义ViewGroup控件(一)----->流式布局进阶(一)
- 最简单的自定义ViewGroup布局控件
- 自己定义ViewGroup控件(一)----->流式布局进阶(一)
- Android常用UI控件 ViewGroup
- 自己定义ViewGroup控件(二)----->流式布局进阶(二)
- Android 自定义ViewGroup布局控件,垂直滚动引导
- 自定义ViewGroup控件(三)----->流式布局进阶(三)
- Android UI布局之用户界面概述
- 自定义ViewGroup控件(二)----->流式布局进阶(二)
- 自定义ViewGroup控件(四)----->流式布局进阶(四)
- Ranger Restful API操作用户user和群组group接口