Android自定义“图片+文字”控件四种实现方法

原文地址: http://www.shangxueba.com/jingyan/1863494.html



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

    [html]

   <linearlayout< p="">

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

   <imageview< p="">

    android:id="@+id/image"

    android:layout_gravity="center_horizontal"

    android:layout_width="80dp"

    android:layout_height="80dp"/>

   <textview< p="">

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

   

    第二部分:整个程序的布局文件,也就是一个gallery:

    [html]

   

   <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" <="" p="">

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

   <textview< p="">

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello" />

   <gallery< p="">

    android:id="@+id/myGallery"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"/>

   

    第三部分:主程序:

    [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;

    } 

    class ViewHolder {

    private ImageView pic;

    private TextView text;

    }

    }

    }

    效果图(图截的有点小,⊙﹏⊙b汗):

    


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值