`

Android:ListView使用RelativeLayout与TableLayout比较

阅读更多

方法一 : ListView使用RelativeLayout

首先,先来看看本文代码运行的结果,Item多出左边的图标:



 
main.xml的源代码,跟上一篇的一样,这里就不作解释了,直接贴出item.xml的代码,就是它实现ImageItem的UI:

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout   
         android:layout_width="fill_parent"   
         xmlns:android="http://schemas.android.com/apk/res/android"   
         android:layout_height="wrap_content"   
         android:paddingBottom="4dip"   
         android:paddingLeft="12dip">  
         <ImageView   
               android:layout_width="wrap_content"   
               android:id="@+id/itemImage" android:layout_height="fill_parent">   
         </ImageView>  
         <TextView   
               android:text="TextView01"   
               android:layout_height="wrap_content"   
               android:layout_width="fill_parent"   
               android:id="@+id/itemTitle" android:layout_toRightOf="@+id/itemImage" android:textSize="20dip">  
         </TextView>  
         <TextView   
               android:text="TextView02"   
               android:layout_height="wrap_content"   
               android:layout_width="fill_parent"   
               android:id="@+id/itemText" android:layout_toRightOf="@+id/itemImage" android:layout_below="@+id/itemTitle">  
         </TextView>  
</RelativeLayout> 

 
解释一下 item.xml的代码:

这里使用了RelativeLayout布局,控件的关键的属性是:
itemTitle的属性

android:layout_toRightOf="@+id/itemImage" ,itemTitle在itemImage的右边;
itemText的属性

android:layout_toRightOf="@+id/itemImage",ItemText在itemImage的右边, android:layout_below="@+id/itemTitle", itemText 在 itemTitle的下面。


最后,贴出JAVA的源代码,其中重点是LayoutInflate的用法。LayoutInflate的使用方法如下:
LayoutInflater的作用是,把一个View的对象与XML布局文件关联并实例化。
View的对象实例化之后,可以通过findViewById()查找布局文件中的指定Id的组件。

package com.testListView;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class testListView extends Activity {
	ListView listView;
	String[] titles={"标题1","标题2","标题3","标题4"};
	String[] texts={"文本内容A","文本内容B","文本内容C","文本内容D"};
	int[] resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.setTitle("BaseAdapter for ListView");
		listView=(ListView)this.findViewById(R.id.listView1);
		listView.setAdapter(new ListViewAdapter(titles,texts,resIds));
	}

	public class ListViewAdapter extends BaseAdapter {
		View[] itemViews;

		public ListViewAdapter(String[] itemTitles, String[] itemTexts,
				int[] itemImageRes) {
			itemViews = new View[itemTitles.length];

			for (int i = 0; i < itemViews.length; i++) {
				itemViews[i] = makeItemView(itemTitles[i], itemTexts[i],
						itemImageRes[i]);
			}
		}

		public int getCount() {
			return itemViews.length;
		}

		public View getItem(int position) {
			return itemViews[position];
		}

		public long getItemId(int position) {
			return position;
		}

		private View makeItemView(String strTitle, String strText, int resId) {
			LayoutInflater inflater = (LayoutInflater) testListView.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			// 使用View的对象itemView与R.layout.item关联
			View itemView = inflater.inflate(R.layout.item, null);

			// 通过findViewById()方法实例R.layout.item内各组件
			TextView title = (TextView) itemView.findViewById(R.id.itemTitle);
			title.setText(strTitle);
			TextView text = (TextView) itemView.findViewById(R.id.itemText);
			text.setText(strText);
			ImageView image = (ImageView) itemView.findViewById(R.id.itemImage);
			image.setImageResource(resId);
			
			return itemView;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null)
				return itemViews[position];
			return convertView;
		}
	}

}

 

参考地址 : http://blog.csdn.net/hellogv/archive/2009/09/13/4548659.aspx

 

方法二 : ListView使用TableLayout

在我实现此种效果时 , 参考了TableLayout,代码量很多 , 可读性很差 . 贴出来做一个比较(ListView中的条目代码,与上面的item.xml的代码实现的效果类似), 做一个比较:

 

<?xml version="1.0" encoding="utf-8"?>
<!-- @author wang_qs  -->
<!-- @update 相比其他的布局而言,此布局为辅助布局,详见联机交易列表中的SimpleAdapter类  -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	android:paddingBottom="3dip" android:paddingTop="3dip">
	<TableRow android:paddingLeft="5dip">
		<ImageView android:src="@drawable/txn_list_item_pic"
			android:id="@+id/item_image" android:layout_centerHorizontal="true"
			android:paddingLeft="10dip" android:paddingTop="5dip"
			android:paddingBottom="5dip" android:layout_gravity="center"
			android:layout_width="wrap_content" android:layout_height="wrap_content">
		</ImageView>
		<TableLayout android:paddingLeft="8dip">
			<TableRow>
				<TextView android:text="商户名称:"  android:paddingTop="8dip"
					android:layout_centerHorizontal="true" android:textSize="14dip"
					android:layout_width="wrap_content" android:layout_height="wrap_content"
					android:textColor="#000000"></TextView>
				<TextView android:id="@+id/txn_list_item_mernm"
					android:textSize="14dip" android:layout_centerHorizontal="true"
					android:layout_width="wrap_content" android:layout_height="wrap_content"
					android:textColor="#dd6600"></TextView>
			</TableRow>
			<TableRow>
				<TextView android:text="日      期:"
					android:layout_centerHorizontal="true" android:textSize="14dip"
					android:layout_width="wrap_content" android:layout_height="wrap_content"
					android:textColor="#000000"></TextView>
				<TextView android:id="@+id/txn_list_item_date"
					android:layout_centerHorizontal="true" android:textSize="14dip"
					android:layout_width="wrap_content" android:layout_height="wrap_content"
					android:textColor="#dd6600"></TextView>
			</TableRow>
			<TableRow>
				<TextView android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:text="金      额:"
					android:textSize="14dip" android:textColor="#000000">
				</TextView>
				<TextView android:id="@+id/txn_list_item_amt"
					android:paddingRight="2dip" android:paddingLeft="0dip"
					android:textSize="14dip" android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:textColor="#dd6600">
				</TextView>
			</TableRow>
		</TableLayout>
	</TableRow>
</TableLayout>

 

 

代码主要是TableLayout与TableRow,TableLayout和TableLayout的嵌套使用 , 上面代码实现的效果图:

 


 

说明 : 在我代码中改变 , 企图在TableLayout中使用TextView的属性android:layout_toRightOf , 但是没有智能识别 , 但是写上去并不报错 , 或许可以中和上面两种方式实现的第三种布局 , 期待测试中 !!
 

  • 大小: 22.1 KB
  • 大小: 25.4 KB
1
1
分享到:
评论
1 楼 kanme818 2012-02-22  
ListView里面如果是嵌套RelativeLayout的时候,如果RelativeLayout的高度和宽度都是fill_parent好像会在onMeasure()时报NullPointException。不知道android:layout_height="wrap_content"这样会不会就不报错了。回去试试看

相关推荐

    android之layout(二)RelativeLayout, TableLayout

    6. **性能考虑**:TableLayout适合显示固定数量的行和列,如果数据动态变化,应考虑使用ListView或RecyclerView等更高效的解决方案。 在实际开发中,开发者通常会结合使用多种布局管理器以达到理想的设计效果。理解...

    Android移动应用开发表格布局TableLayout的常用属性.pdf

    需要注意的是,虽然TableLayout提供了强大的布局能力,但在处理动态数据或大量数据时,使用ListView或RecyclerView可能会更高效,因为它们有更好的滚动性能和内存管理。然而,在需要简单表格布局的场景下,...

    android 使用ListView来实现表格

    - 如果需要更复杂的表格功能,如单元格合并、行高自适应等,可能需要使用第三方库,如`android-tablelayout`或`android-gridlayout`,或者自定义更复杂的Adapter和View。 通过以上步骤,我们就能在Android应用中...

    android捕获ListView中每个item点击事件

    在Android开发中,`ListView`是十分常见的UI组件之一,用于显示可滚动的列表数据。当我们在`ListView`中放置一系列的数据项(通常称为item)时,通常还需要为这些item添加点击事件监听器以便在用户点击某个item时...

    Android Layout

    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"&gt; android:id="@+...

    android界面开发

    &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"&gt; android:id="@+id/textView" android:layout_...

    Android开发view的几种布局方式[定义].pdf

    在Android开发中,构建用户界面的关键在于理解和使用各种布局方式。布局方式决定了应用程序视图的组织和展示方式,直接影响用户体验。下面将详细讲解Android中主要的四种布局:线性布局、相对布局、表格布局和列表...

    实验2android的界面设计(控件与布局)[整理].pdf

    本实验主要涉及了Android界面设计的基础,包括控件的使用和布局的设计。以下是对实验内容的详细解析: 1. **基本控件的制作**: - **TextView**:TextView用于展示文本信息,可以设置Autolink属性来自动识别并链接...

    design tablelayout demo

    在Android 5.0之后,Material Design设计语言成为主流,TableLayout在新版本中依然可用,但可能会与Material Design的规范有所冲突。因此,在设计时需要确保与系统的视觉风格保持一致。 总之,TableLayout是Android...

    Android布局

    DatePicker组件用于让用户选择日期,通常与DialogFragment一起使用。开发者可以通过`DatePicker.setMinDate()`和`DatePicker.setMaxDate()`来限制日期范围。 7. **ListView**: ListView是展示大量数据的滚动列表...

    牛人android入门

    - **Android SDK(Software Development Kit)**:Android开发的核心工具包,包含了必要的API、文档以及开发工具。 - **Eclipse + ADT(Android Development Tools)**:早期Android开发常用的集成开发环境。虽然...

    Android七种layout布局实例(可直接运行)

    例如,在一个活动中,可能需要先使用LinearLayout或RelativeLayout来整体布局,然后在特定区域嵌入一个TableLayout展示表格数据,再添加一个TabLayout来分页显示内容,最后在底部使用GridView或ListView展示一系列可...

    Android实验指导.doc

    4. **ADT**:Android Development Tools,Eclipse的插件,为Android开发提供图形化界面和自动化工具。 5. **手机USB驱动**:如果选择在真实设备上运行应用,需要安装对应的USB驱动,以便通过USB连接进行调试。 完成...

    App开发期中总结作业

    &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"&gt; android:id="@+id/imageView" android:layout_...

    《第一行Android代码》课件:第三章 UI点点滴滴.pptx

    Android提供了四种基本布局:LinearLayout(线性布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)和TableLayout(表格布局)。这些布局可以帮助开发者灵活地组织和排列控件。 3.4 自定义控件 当系统默认...

    Android_布局详解【图文】

    选项卡布局常与ViewPager结合使用,用于创建滑动的页面,每个页面对应一个选项卡。例如: ```xml &lt;androidx.tabs.TabLayout ...&gt; &lt;com.google.android.material.tabs.TabItem ... /&gt; &lt;/androidx.tabs....

Global site tag (gtag.js) - Google Analytics