Android 九宫格布局文件.docx
《Android 九宫格布局文件.docx》由会员分享,可在线阅读,更多相关《Android 九宫格布局文件.docx(9页珍藏版)》请在冰豆网上搜索。
Android九宫格布局文件
AndroidGridView九宫格图片加文字
首先是main.xml的布局方式如下:
xml version="1.0" encoding="utf-8"?
>
--主界面的布局-->
xmlns:
android="
android:
orientation="vertical"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent" >
android:
id="@+id/MainActivityrlTwo"
android:
layout_width="fill_parent"
android:
layout_height="45dp" >
android:
id="@+id/MainActivityGrid"
android:
layout_width="fill_parent"
android:
layout_height="wrap_content"
android:
numColumns="3"
android:
columnWidth="50dp"
android:
layout_below="@+id/MainActivityrlTwo"
android:
layout_marginTop="5dp" />
android:
id="@+id/MainActivityrlThree"
android:
layout_width="fill_parent"
android:
layout_height="60dp"
android:
layout_alignParentBottom="true" >
android:
id="@+id/tvLineBottom"
android:
layout_width="fill_parent"
android:
layout_height="wrap_content"
android:
text="@string/line_default" />
其次就是每一格九宫格的布局方式:
xml version="1.0" encoding="utf-8"?
>
--九宫格每一格的布局-->
xmlns:
android="
android:
orientation="vertical"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent" >
android:
id="@+id/MainActivityImage"
android:
layout_width="50dp"
android:
layout_height="50dp"
android:
layout_gravity="center_horizontal" />
android:
id="@+id/MainActivityText"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_gravity="center_horizontal"
android:
textSize="18sp"
android:
lines="1"
android:
layout_marginTop="8dp" />
最后就是adapter的编写:
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context) {
this.context=context;
}
private Integer[] images = {
//九宫格图片的设置
R.drawable.icon_1,
R.drawable.icon_2,
R.drawable.icon_3,
R.drawable.icon_4,
R.drawable.icon_5,
R.drawable.icon_6,
R.drawable.icon_7,
R.drawable.icon_8,
R.drawable.icon_9,
};
private String[] texts = {
//九宫格图片下方文字的设置
"记录支出",
"记录收入",
"账本管理",
"类别管理",
"查看图表",
"收支对照",
"记录心得",
"新闻公告",
"系统设置",
};
//get the number
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return position;
}
//get the current selector's id number
@Override
public long getItemId(int position) {
return position;
}
//create view method
@Override
public View getView(int position, View view, ViewGroup viewgroup) {
ImgTextWrapper wrapper;
if(view==null) {
wrapper = new ImgTextWrapper();
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.item, null);
view.setTag(wrapper);
view.setPadding(15, 15, 15, 15); //每格的间距
} else {
wrapper = (ImgTextWrapper)view.getTag();
}
wrapper.imageView = (ImageView)view.findViewById(R.id.MainActivityImage);
wrapper.imageView.setBackgroundResource(images[position]);
wrapper.textView = (TextView)view.findViewById(R.id.MainActivityText);
wrapper.textView.setText(texts[position]);
return view;
}
}
class ImgTextWrapper {
ImageView imageView;
TextView textView;
}
-main
publicclassMainActivityextendsActivity{
GridViewgridView;
ImageAdapteradapter;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView)findViewById(R.id.MainActivityGrid);
adapter=newImageAdapter(this);
gridView.setAdapter(adapter);
}
}
xmlversion="1.0"encoding="utf-8"?
>
android="
android:
id="@+id/gridview"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
android:
columnWidth="90dp"
android:
gravity="center"
android:
horizontalSpacing="10dp"
android:
numColumns="auto_fit"
android:
stretchMode="columnWidth"
android:
verticalSpacing="10dp"/>
xmlversion="1.0"encoding="utf-8"?
>
xmlns:
android="
android:
layout_height="wrap_content"
android:
paddingBottom="4dip"android:
layout_width="fill_parent"
android:
gravity="center_horizontal">
android:
id="@+id/shoukuan"
android:
layout_width="70dp"
android:
layout_height="wrap_content"
android:
layout_weight="1"
android:
gravity="center"
android:
orientation="vertical">
android:
layout_width="match_parent"
android:
layout_height="20dp"/>
android:
id="@+id/img_shoukuan"
android:
layout_width="30dp"
android:
layout_height="30dp"
android:
layout_weight="1"
android:
src="@drawable/o2o_16"/>
android:
layout_width="match_parent"
android:
layout_height="20dp"/>
android:
id="@+id/txt_shoukuan"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_weight="1"
android:
text="O2O收款"
android:
textColor="#000000"
android:
textSize="13dp"
android:
gravity="center"/>
publicclassMainActivityextendsActionBarActivity{
//定义图标数组
privateint[]imageRes={
R.drawable.o2o_16,
R.drawable.dingdan_16,
R.drawable.zhuanzhang_16,
R.drawable.telchongzhi_28,
R.drawable.card_30,
R.drawable.shuidian_31,
R.drawable.weizhang_39,
R.drawable.kuaidi_39,
R.drawable.jiahao_45};
//定义图标下方的名称数组
privateString[]name={
"O2O收款",
"订单查询",
"转账",
"手机充值",
"信用卡还款",
"水电煤",
"违章代缴",
"快递查询",
"更多"
};
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridViewgridview=(GridView)findViewById(R.id.gridview);
intlength=imageRes.length;
//生成动态数组,并且转入数据
ArrayList>lstImageItem=newArrayList>();
for(inti=0;iHashMapmap=newHashMap();
map.put("ItemImage",imageRes[i]);//添加图像资源的ID
map.put("ItemText",name[i]);//按序号做ItemText
lstImageItem.add(map);
}
//生成适配器的ImageItem与动态数组的元素相对应
SimpleAdaptersaImageItems=newSimpleAdapter(this,
lstImageItem,//数据来源
R.layout.item,//item的XML实现
//动态数组与ImageItem对应的子项
newString[]{"ItemImage","ItemText"},
//ImageItem的XML文件里面的一个ImageView,两个TextViewID
newint[]{R.id.img_shoukuan,R.id.txt_shoukuan});
//添加并且显示
gridview.setAdapter(saImageItems);
//添加消息处理
gridview.setOnItemClickListener(newAdapterView.OnItemClickListener(){
@Override
publicvoidonItemClick(AdapterView
>parent,Viewview,intposition,longid){
Toast.makeText(MainActivity.this,name[position],Toast.LENGTH_LONG).show();
}
});
}
}