Android中使用GridView实现底部标签页效果
来源:互联网 发布:php 站点根目录 编辑:程序博客网 时间:2024/06/15 07:20
底部标签页效果如下:
在布局文件中,定义如下:
<GridView android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" android:numColumns="5" />
全局中定义几个变量:
private GridView grid; private View[] vs=new View[5]; private ImageView[] imgs = new ImageView[5]; private TextView[] tvs = new TextView[5]; private String[] str = {"案例","模型","进度","资料","我的"}; private int[] resOff={R.drawable.icon_anli_off,R.drawable.icon_moxing_off,R.drawable.icon_jindu_off,R.drawable.icon_ziliao_off,R.drawable.icon_my_off}; private int[] resOn={R.drawable.icon_anli_on,R.drawable.icon_moxing_on,R.drawable.icon_jindu_on,R.drawable.icon_ziliao_on,R.drawable.icon_my_on};
下面在OnCreate函数中:
for (int i = 0; i < vs.length; i++) { vs[i] = getLayoutInflater().inflate(R.layout.act_grid_item,null); imgs[i] = (ImageView) vs[i].findViewById(R.id.item_img); tvs[i]=(TextView) vs[i].findViewById(R.id.item_tv); imgs[i].setImageResource(resOff[i]); tvs[i].setText(str[i]); } imgs[0].setImageResource(resOn[0]); tvs[0].setTextColor(Color.parseColor("#0099cc")); grid = (GridView) findViewById(R.id.grid); grid.setAdapter(new MyAdapter());
GridView中每一个Item点击后,被点击的条目高亮蓝色显示,其它条目灰色显示,实现代码如下:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { for (int i = 0; i < 2; i++) { if(i==position){ imgs[i].setImageResource(resOn[i]); tvs[i].setTextColor(Color.parseColor("#0099cc")); }else{ imgs[i].setImageResource(resOff[i]); tvs[i].setTextColor(Color.parseColor("#000000")); } }}
再看看Adapter的具体应用:
class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return vs.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return vs[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub return vs[position]; } }
值得注意的是,平时我们往GridView的Adapter中传入的要么是int数组或者是String数组或者是Bean列表,本篇文章的例子发现,传入的还可以是一个View数组
act_grid_item.xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:background="#ffffff" android:padding="5dp" > <ImageView android:id="@+id/item_img" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/icon_anli_off" /> <TextView android:id="@+id/item_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="案例" android:textColor="#000" android:textSize="16sp" /></LinearLayout>
FR:徐海涛(Hunk Xu) QQ群:386476712
1 0
- Android中使用GridView实现底部标签页效果
- Android 利用GridView来实现一个底部标签页的效果
- Android Fragment应用实战,使用碎片向ActivityGroup说再见(使用Fragment实现底部标签页效果)
- android中fragment实现底部标签页的切换
- 实现android studio底部导航栏四个标签的效果
- Android中使用TabHost实现类似标签栏的效果
- Android中GridView的书架效果实现
- Android中使用FragmentTabHost实现底部导航
- Android中GridView实现标签只选能一个功能
- Android GridView实现动画效果
- Android--------使用BottomTabBar实现底部导航页
- Android--使用BottomTabBar实现底部导航页
- Android--------使用BottomTabBar实现底部导航页
- Android--------使用BottomTabBar实现底部导航页
- Android---使用BottomTabBar实现底部导航页
- Android--------使用BottomTabBar实现底部导航页
- Android UI-实现底部切换标签(fragment)
- Android UI-实现底部切换标签(fragment)
- android摄像头开发,将Camera.onPreviewFrame里面的data转换成bitmap
- ROS nodelets-----简介和简单应用
- 文章标题
- 使用retrofit与Rxjava结合出的错误:java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker
- 多行文本溢出显示省略号(...)的方法
- Android中使用GridView实现底部标签页效果
- php数组函数总结
- uva1267 Network
- javaScript 对象转JSON,Java String 转JSON对象
- jq添加移除元素
- java 的三种移位方式的本质
- 【unity小技巧之五】unity编辑器脚本添加类目到unity Preferences中
- iOS 动画效果:Core Animation & Facebook
- 用MyEclipse10.0开发基于JAX-WS的Web Service实例