九宫格
**在这次作业中老师要求我们做出手机中常用的九宫格界面,此时我们需要使用GridView和SimpleAdapter来实现手机界面的九宫格,看似很简单,做起来却并不是很容易。首先我们要打开一个空白布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/w"
android:orientation="vertical"
tools:context="com.example.mpyypm.qwer.MainActivity">
</LinearLayout>
其效果如图:
**其次,我们来做出显示图片的代码运用ImageView,只需做出一张图片剩下的复制粘贴即可,图片的代码如下;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mpyypm.qwer.MainActivity">
<ImageView
android:id="@+id/n"
android:layout_width="100dp"
android:layout_height="200dp">
android:gravity="center"
</ImageView>
</LinearLayout>
**再其次,我们要使用GridView来实现九宫格的格式,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mpyypm.qwer.MainActivity">
<GridView
android:id="@+id/z"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:gravity="center"
android:horizontalSpacing="20dp"
android:numColumns="5"
android:verticalSpacing="10dp">
</GridView>
</LinearLayout>
其实现效果如图:
第二部分就是Java部分的代码了:
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView) findViewById(R.id.gridview);
adapter = new SimpleAdapter(
this,
getData(),
R.layout.photo_item,
new String[]{"imageview"},
new int[]{R.id.imageview});
gridView=(GridView) findViewById(R.id.gridview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String data = (String) adapterView.getItemAtPosition(position);
}
});
}
private List<HashMap<String, Object>> getData() {
List<HashMap<String, Object>> datas = new ArrayList<>();
HashMap<String, Object> data = new HashMap<>();
data.put("imageview", R.drawable.a);
datas.add(data);
data=new HashMap<>();
data.put("imageview", R.drawable.b);
datas.add(data);
data=new HashMap<>();
data.put("imageview", R.drawable.c);
datas.add(data);
data=new HashMap<>();
data.put("imageview", R.drawable.d);
datas.add(data);
data=new HashMap<>();
data.put("imageview", R.drawable.e);
datas.add(data);
data=new HashMap<>();
data.put("imageview", R.drawable.f);
datas.add(data);
data=new HashMap<>();
data.put("imageview", R.drawable.g);
datas.add(data);
data=new HashMap<>();
data.put("imageview", R.drawable.h);
datas.add(data);
data=new HashMap<>();
data.put("imageview", R.drawable.i);
datas.add(data);
return datas;
}
}
即可得到想要的九宫格模板。
浙公网安备 33010602011771号