九宫格
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="com.example.yz.jiugongge.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/yz"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
GrideView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
tools:context="com.example.yz.jiugongge.MainActivity">
<GridView
android:layout_width="match_parent"
android:numColumns="3"
android:layout_height="match_parent"
android:id="@+id/view"
>
</GridView>
</LinearLayout>
SimpleAdapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="150dp"
android:id="@+id/image"
/>
</LinearLayout>
MainActivity
package com.example.yz.jiugongge;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
Activity gridFragment =new Activity();
fragmentTransaction.add(R.id.yz,gridFragment);
fragmentTransaction.commit();
}
}
Activity
package com.example.yz.jiugongge;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Activity extends Fragment {
private SimpleAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.gridview, container, false);
adapter=new SimpleAdapter(getActivity(),getData(),R.layout.simpleadapter,
new String[]{"img"},
new int[]{R.id.image}
);
GridView gridView= (GridView)view.findViewById(R.id.view);
gridView.setAdapter(adapter);
return view;
}
private List<HashMap<String, Object>> getData(){
List<HashMap<String,Object>>datas = new ArrayList<>();
HashMap<String,Object> data = new HashMap<>();
//Map映射添加数据
data.put("img",R.mipmap.ic_launcher);
//将Map放到list
datas.add( data);
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
datas.add( data);
//将Map放到list
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
datas.add( data);
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
datas.add( data);
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
datas.add( data);
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
datas.add( data);
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
datas.add( data);
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
datas.add( data);
data = new HashMap<>();
data.put("img",R.mipmap.ic_launcher);
datas.add( data);
return datas;
}
}