第二种方法:GridView+ActivityGroup (图片 ,文字)
(为了省事,我把上下tab分页整理到一个demo里面了.)
这个的布局文件我就不显示了,因为比较简单,我们还是来看代码吧.
代码片段:
- /***
- * 适配器
- *
- * @author Administrator
- *
- */
- public class ImageAdapter extends BaseAdapter {
- private Context mContext;
- private ImageTextButton[] imgItems;
- private int selResId;
- /***
- *
- * @param c
- * @param picIds
- * @param titles
- * @param width
- * @param height
- * @param selResId
- */
- public ImageAdapter(Context c, int[] picIds, String titles[], int width,
- int height, int selResId) {
- mContext = c;
- this.selResId = selResId;
- imgItems = new ImageTextButton[picIds.length];
- for (int i = 0; i < picIds.length; i++) {
- imgItems[i] = new ImageTextButton(mContext);
- imgItems[i]
- .setLayoutParams(new GridView.LayoutParams(width, height));// 设置ImageView宽高
- imgItems[i].setPadding(2, 2, 2, 2);
- // 显示图片与文本
- imgItems[i].setImageResource(picIds[i], titles[i]);
- }
- }
- @Override
- public int getCount() {
- return imgItems.length;
- }
- @Override
- public Object getItem(int position) {
- return position;
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- /***
- * 设置选中后的效果
- */
- public void SetFocus(int index) {
- for (int i = 0; i < imgItems.length; i++) {
- // 先把所有设为最初状态
- if (i != index) {
- imgItems[i].setBackgroundResource(0);// 回到最初样式
- }
- }
- // 选中设置
- imgItems[index].setBackgroundResource(selResId);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageTextButton imageView;
- if (convertView == null) {
- imageView = imgItems[position];
- } else {
- imageView = (ImageTextButton) convertView;
- }
- return imageView;
- }
- }
在这里我们用到了自定义控件,其实就是把imageview 和textview 整到一起了,
- /***
- * 自定义控件(图片文字)
- */
- public class ImageTextButton extends LinearLayout {
- private ImageView button = null;
- private TextView text = null;
- private Context context;
- public ImageTextButton(Context context) {
- this(context, null);
- this.context = context;
- }
- public ImageTextButton(Context context, AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(context).inflate(R.layout.imagetextbutton, this,
- true);
- button = (ImageView) this.findViewById(R.id.button);
- text = (TextView) this.findViewById(R.id.btnText);
- text.setSingleLine(true);
- }
- public void setImageResource(int image_id, String title) {
- Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
- image_id);
- button.setBackgroundDrawable(new BitmapDrawable(bitmap));
- text.setText(title);
- }
- public void setImageBitmap(Bitmap bitmap) {
- if (button != null)
- button.setImageBitmap(bitmap);
- }
- public void setBackgroundDrawable(Drawable drawable, int Width, int Hdight) {
- if (button != null) {
- button.setBackgroundDrawable(drawable);
- button.setMinimumHeight(Hdight);
- button.setMinimumWidth(Width);
- }
- }
- public void setText(String title) {
- if (text != null)
- text.setText(title);
- }
- public void setText(int ResID) {
- if (text != null)
- text.setText(ResID);
- }
- public void setWidth(int width) {
- button.setMaxWidth(width);
- }
- public void setHeight(int height) {
- button.setMaxHeight(height);
- }
- }
我们只需要在oncreate中调用即可:
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Navigation_Top_Bar = (GridView) this
- .findViewById(R.id.Navigation_Top_Bar);
- Navigation_Buttom_Bar = (GridView) this
- .findViewById(R.id.Navigation_Buttom_Bar);
- // 获取显示宽度
- int width = this.getWindowManager().getDefaultDisplay().getWidth()
- / topbar_image_array.length;
- topImgAdapter1 = new ImageAdapter(this, topbar_image_array, titles,
- width, 100, R.drawable.cover);
- Init(Navigation_Top_Bar, topImgAdapter1);
- ButtomImgAdapter2 = new ImageAdapter(this, topbar_image_array, titles,
- width, 100, R.drawable.cover);
- Init(Navigation_Buttom_Bar, ButtomImgAdapter2);
- }
这个实现起来有点复杂,不过用习惯了会觉得别有一翻风味的.我之前就一直用这个方法.
在这里我要说明一点:
imgItems[i].setLayoutParams(new GridView.LayoutParams(width, height));// 设置ImageView宽高
其他的都是细节上的问题,我想你们看过都会ok的.
效果图:
(怎么样,效果还不错吧。就是实现起来有点负责,不过习惯就好.)
