展会信息港展会大全

android学习笔记21--------------Android中RelativeLayout+ListView
来源:互联网   发布日期:2015-11-27 13:29:46   浏览:1231次  

导读:效果图 public class ListViewActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main);...

效果图

public class ListViewActivity extends ListActivity

{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

// setContentView(R.layout.main);

SimpleAdapter adapter = new SimpleAdapter(this, getData(),

R.layout.main, new String[]

{"title","info","img"}, new int[]

{ R.id.title, R.id.info, R.id.img });

setListAdapter(adapter);

}

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", "G1");

map.put("info", "google 1");

map.put("img", R.drawable.icon);

list.add(map);

map = new HashMap<String, Object>();

map.put("title", "G2");

map.put("info", "google 2");

map.put("img", R.drawable.icon);

list.add(map);

map = new HashMap<String, Object>();

map.put("title", "G3");

map.put("info", "google 3");

map.put("img", R.drawable.icon);

list.add(map);

return list;

}

}

public class ListViewActivity extends ListActivity

{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

// setContentView(R.layout.main);

SimpleAdapter adapter = new SimpleAdapter(this, getData(),

R.layout.main, new String[]

{"title","info","img"}, new int[]

{ R.id.title, R.id.info, R.id.img });

setListAdapter(adapter);

}

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", "G1");

map.put("info", "google 1");

map.put("img", R.drawable.icon);

list.add(map);

map = new HashMap<String, Object>();

map.put("title", "G2");

map.put("info", "google 2");

map.put("img", R.drawable.icon);

list.add(map);

map = new HashMap<String, Object>();

map.put("title", "G3");

map.put("info", "google 3");

map.put("img", R.drawable.icon);

list.add(map);

return list;

}

}

[java]

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

<RelativeLayout

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

<ImageView

android:id="@+id/img"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="2px"/>

<TextView

android:id="@+id/title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/img"

android:textColor="#FFFFFFFF"

android:textSize="22px" />

<TextView

android:id="@+id/info"

android:layout_toRightOf="@id/img"

android:layout_alignBottom="@id/img"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#FFFFFFFF"

android:textSize="13px" />

</RelativeLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

<RelativeLayout

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

<ImageView

android:id="@+id/img"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="2px"/>

<TextView

android:id="@+id/title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/img"

android:textColor="#FFFFFFFF"

android:textSize="22px" />

<TextView

android:id="@+id/info"

android:layout_toRightOf="@id/img"

android:layout_alignBottom="@id/img"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#FFFFFFFF"

android:textSize="13px" />

</RelativeLayout>

</LinearLayout>

ListActivity下有setListAdapter()方法

// ListView www.2cto.com中某项被选中后的逻辑

[java]

<pre name="code" class="java">@Override

protected void onListItemClick(ListView l, View v, int position, long id)

{

Log.v("MyListView4-click", (String) mData.get(position).get("title"));

}

<pre name="code" class="java"> @Override

protected void onListItemClick(ListView l, View v, int position, long id)

{

Log.v("MyListView4-click", (String) mData.get(position).get("title"));

}

摘自 奔跑的蜗牛

赞助本站

人工智能实验室

相关热词: android开发 教程

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港