如果您想要创建一个包含5列的简单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=".MainActivity">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</RelativeLayout>
添加您的活动和适配器代码。如果您想要为视图添加动画效果,可以使用Android GridLayout Manager与Material Design中的Recycler View,如下所示:
public class MainActivity extends ActionBarActivity {
private GridLayoutManager lLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle(null);
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
topToolBar.setLogo(R.drawable.logo);
topToolBar.setLogoDescription(getResources().getString(R.string.logo_desc));
List<ItemObject> rowListItem = getAllItemList();
lLayout = new GridLayoutManager(MainActivity.this, 4);
RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, rowListItem);
rView.setAdapter(rcAdapter);
}
private List<ItemObject> getAllItemList(){
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject("United States", R.drawable.one));
allItems.add(new ItemObject("Canada", R.drawable.two));
allItems.add(new ItemObject("United Kingdom", R.drawable.three));
allItems.add(new ItemObject("Germany", R.drawable.four));
allItems.add(new ItemObject("Sweden", R.drawable.five));
allItems.add(new ItemObject("United Kingdom", R.drawable.six));
allItems.add(new ItemObject("Germany", R.drawable.seven));
allItems.add(new ItemObject("Sweden", R.drawable.eight));
allItems.add(new ItemObject("United States", R.drawable.one));
allItems.add(new ItemObject("Canada", R.drawable.two));
allItems.add(new ItemObject("United Kingdom", R.drawable.three));
allItems.add(new ItemObject("Germany", R.drawable.four));
allItems.add(new ItemObject("Sweden", R.drawable.five));
allItems.add(new ItemObject("United Kingdom", R.drawable.six));
allItems.add(new ItemObject("Germany", R.drawable.seven));
allItems.add(new ItemObject("Sweden", R.drawable.eight));
return allItems;
}
}