当前位置:操作系统 > 安卓/Android >>

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

要想做图片+文字这种复合控件,实现方法大概有四种。第一种就是利用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);  
     
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,