GridView

GridView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.crazyit.myapplication_gridviewdemo.MainActivity">

<GridView
android:id="@+id/gvInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="spacingWidthUniform"
android:gravity="center"
>

</GridView>
</RelativeLayout>

----------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp">

<ImageView
android:id="@+id/ItemImage"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:scaleType="fitXY"
android:padding="4dp"/>
<TextView
android:id="@+id/ItemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"/>

</LinearLayout>

----------------------------------------------------------

package org.crazyit.myapplication_gridviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity {
GridView gvInfo;
String[] titles=new String[]{"新增支出","新增收入","我的支出","我的收入","数据管理","系统设置","收支便签","帮助","退出"};
int [] images=new int[]{R.drawable.accountflag,R.drawable.addinaccount,R.drawable.addoutaccount,R.drawable.exit,R.drawable.help,
R.drawable.inaccountinfo,R.drawable.outaccountinfo,R.drawable.showinfo,R.drawable.sysset};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gvInfo=(GridView)findViewById(R.id.gvInfo);
List<Map<String,Object>> listItems=new ArrayList<Map<StringObject>>();
for (int i=0;i<images.length;i++){
Map<String,Object> map=new HashMap<StringObject>();
map.put("image",images[i]);
map.put("title",titles[i]);
listItems.add(map);
}
SimpleAdapter adapter=new SimpleAdapter(this,listItems,R.layout.gvitem,new String[]{"title","image"},new int[]{R.id.ItemTitle,R.id.ItemImage});
gvInfo.setAdapter(adapter);
}
}   
图片
阅读更多
下一篇第一行代码 聊天
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭