展会信息港展会大全

用Gallery实现带文字提示的显示效果 android教程
来源:互联网   发布日期:2015-10-03 10:42:27   浏览:1934次  

导读:实现的效果类似这样:和上周实现的实现横向的类似Gallery的效果不同,那是用scrollview和tablelayout实现的,事后发现当横向滚屏选择图片的时候,如果有纵向位移,也会造成下面列表的纵向滚动。时间紧急,因此还......

实现的效果类似这样:

和上周实现的实现横向的类似Gallery的效果不同,那是用scrollview和tablelayout实现的,事后发现当横向滚屏选择图片的时候,如果有纵向位移,也会造成下面列表的纵向滚动。

时间紧急,因此还是先用Gallery来实现类似效果吧。

Gallery的布局文件(table_title.xml):

<?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">

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="选择电影" />

<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"

android:gravity="center_horizontal" android:spacing="10px"

android:layout_height="wrap_content" android:layout_marginTop="20px" ></Gallery>

</LinearLayout>

Gallery内部的一个条目,也就是包括图片和下面的文字,布局(image_item.xml):

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

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

android:orientation="vertical" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:background="#ffffffff">

<ImageView android:layout_width="wrap_content"

android:layout_height="wrap_content" android:src="@drawable/k"/>

<TextView android:layout_width="match_parent"

android:layout_height="wrap_content" android:text="清.康熙" android:gravity="center_horizontal"/>

</LinearLayout>

可以看到都是写死的图片和文字。

在Activity中要为Gallery设置Adapter,否则Gallery中是空的:

this.gallery = (Gallery) this.findViewById(R.id.gallery);

this.gallery.setAdapter(new ImageAdapter(this));

ImageAdapter是继承BaseAdapter的类:

class ImageAdapter extends BaseAdapter {

public ImageAdapter(Context context) {

super();

this.context = context;

}

private Context context;

@Override

public int getCount() {

return 7;

}

@Override

public Object getItem(int position) {

return position;

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

LinearLayout layout = (LinearLayout) View.inflate(context,

R.layout.image_item, null);

return layout;

}

}

这里getCount() 方法是假的,返回多少,就会有多少个条目,当然是相同内容的。

源代码见:

http://easymorse.googlecode.com/svn/tags/video.demo-0.4.0/

赞助本站

人工智能实验室

相关热词: Gallery 文字提示

AiLab云推荐
展开

热门栏目HotCates

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