要想做图片+文字这种复合控件,实现方法大概有四种。第一种就是利用Gallery来做。
第一部分:新建一个布局文件,用来放图片加文字。名字为:pic_text.xml,内容为:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_gravity="center_horizontal"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="14dp"
android:gravity="center"
android:textColor="#ffffffff"/>
</LinearLayout>
第二部分:整个程序的布局文件,也就是一个gallery:
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Gallery
android:id="@+id/myGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
第三部分:主程序:
package yan.guoqi.testgallery;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class TestGalleryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery)findViewById(R.id.myGallery);
gallery.setAdapter(new galleryAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(TestGalleryActivity.this,
""+id+"被点击!",
Toast.LENGTH_SHORT).show();
}
});
gallery.setSelection(1);
gallery.setSpacing(20);
gallery.setUnselectedAlpha(150.0f);
}
public class galleryAdapter extends BaseAdapter{
private Integer[] img = {R.drawable.identify,
R.drawable.recognize,R.drawable.manage};
private String[] str={"认证模块","识别模块","管理掌纹库"};
private Context mContext;
public galleryAdapter(Context c){
mContext = c;
}
public int getCount() {
// TODO Auto-generated method stub
return img.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = View.inflate(mContext, R.layout.pic_text, null);
holder.pic = (ImageView)convertView.findViewById(R.id.image);
holder.text = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
holder.pic.setImageResource(img[position]);
holder.text.setText(str[position]);
return convertView;
}
class ViewHolder {
private ImageView pic;
private TextView text;
}
}
}
效果图(图截的有点小,⊙﹏⊙b汗):
分析:这种实现方式是借助Gallery,自定义一个布局。要自己写适配器,对于学习gallery还算不错。做出来的效果可以滑动。但不推荐这么写,这么写貌似大写小用Gallery了。严重推荐下篇博客里的写法。
源码--http://download.csdn.net/detail/yanzi1225627/5108013
分享到:
相关推荐
Android 使用Gallery_tabhost实现标签效果图源码 Android 例子源码根据CPU使用时间计算程序电量使用 Android 医院介绍电子书籍源码 android 图片涂鸦 Android 基于局域网的P2P聊天系统 Android 天气预报 Android ...
Android自定义Gallery,实现CoverFlow效果 高仿网易新闻顶部滑动条效果 Android源码之动态壁纸引擎 动态桌面实现 android控件的抖动效果 很漂亮的ListView android 图像处理滤镜 照亮边缘特效 无闪烁启动画面 ...
本文实例讲述了Android编程滑动效果之Gallery+GridView实现图片预览功能。分享给大家供大家参考,具体如下: Android系统自带一个GridView和Gallery两个控件,GridView网格显示,Gallery单个浏览,两者结合起来可以...
6.11.4 为Android应用提供国际化 资源 255 6.11.5 国际化Android应用 256 6.12 本章小结 258 第7章 图形与图像处理 259 7.1 使用简单图片 260 7.1.1 使用Drawable对象 260 7.1.2 Bitmap和...
主要包括:缓存(图片缓存、预取缓存、网络缓存)、公共View(下拉及底部加载更多ListView、底部加载更多ScrollView、滑动一页Gallery)及Android常用工具类(网络、下载、Android资源操作、shell、文件、Json、随机数、...
《Android开发案例驱动教程》 配套代码。 注: 由于第12,13,14章代码太大,无法上传到一个包中。 这三节代码会放到其他压缩包中。 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式...
Google Android SDK开发范例大全(完整版) 包含部分书中源码 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 ...
画廊在很多的App设计中都有,如下图所示: 该例子是我没事的时候写的一个小项目,具体源码地址请访问...布局中添加该自定义控件 <RelativeLayout xmlns:android=http://schemas.an
java安卓仿微信聊天软件源码 Android开源项目 Android开源项目,旨在搜集平常工作中遇到和可能...##自定义控件 Layout 伸缩展开布局 视差布局 手势 * TipView/Badge TagView/FlowTag Gallery TimeLineView Loading
android自定义view讲解 给高级 Android 工程师的进阶手册 高质量Android社区 —— 国外著名的问答社区 —— 长期更新最新前言资讯 —— 《Android源码设计模式》作者 Mr.Simple 维护的社区网站 —— 大量第三方控件...
我想到了好几种:自定义ViewPager,修改开源项目CoverFlow ,使用RecyclerView,自定义Gallery控件。其实开源项目CoverFlow 本来就是一个画廊控件,但是其效果是3d倒影效果,滑动的自然程度也不是很理想。我最先尝试...
从效果看,大概就能猜出用什么控件类(支持左右滑动的控件类+GridView),支持左右滑动的控件类,有很多了比如常用的Gallery,ViewPager,ViewFlipper,ViewFlow等等,本例自定义继承ViewGroup的。看过launcher源码的人...
作者BCsl,源码GalleryLayoutManager,用自定义 LayoutManager 实现 Android 中 Gallery 或者 ViewPager 控件的效果,支持 RecycleView 的试图回收机制。