要想做图片+文字这种复合控件,实现方法大概有四种。第一种就是利用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了。严重推荐下篇博客里的写法。