Android自定义“图片+文字”控件四种实现方法之一--------Gallery原理(提供源码下载)

要想做图片+文字这种复合控件,实现方法大概有四种。第一种就是利用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

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值