码迷,mamicode.com
首页 > 其他好文 > 详细

九宫格

时间:2017-05-09 00:40:32      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:tool   res   manage   ash   apk   instance   odi   version   ted   

<?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;
}

}








九宫格

标签:tool   res   manage   ash   apk   instance   odi   version   ted   

原文地址:http://www.cnblogs.com/CCyz/p/6828358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!