展会信息港展会大全

Android自定义“图片+文字”控件四种实现方法之一--------Gallery原理(提供源码下载)
来源:互联网   发布日期:2015-10-03 10:44:33   浏览:1411次  

导读:要想做图片+文字这种复合控件,实现方法大概有四种。第一种就是利用Gallery来做。第一部分:新建一个布局文件,用来放图片加文字。名字为:pic_text.xml,内容为:[html] ...

要想做图片+文字这种复合控件,实现方法大概有四种。第一种就是利用Gallery来做。

第一部分:新建一个布局文件,用来放图片加文字。名字为:pic_text.xml,内容为:

[html]

<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:

[html]

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

第三部分:主程序:

[java]

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;

}www.2cto.com

class ViewHolder {

private ImageView pic;

private TextView text;

}

}

}

效果图(图截的有点小,⊙

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

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