九九九九九宫格

这次的作业为九宫格 ,首先依旧是最基本的布局

 

<RelativeLayout

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"

tools:context="com.example.lenovo.girdview.MainActivity">

<GridView

android:id="@+id/gv"

android:numColumns="3"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</RelativeLayout>

之后再是一个item布局

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="你好"

android:textSize="50sp"

android:layout_gravity="center"/>

</LinearLayout>

然后就是java

public class MainActivity extends AppCompatActivity

{

private GridView gridView;

private MyAdapter myAdapter;

private List<String> list;

private String[] s1 = new String[] { "1111", "2222", "3333", "4444", "5555", "6666", "7777", "8888" , "9999"};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

list=new ArrayList<String>();

for (int i=0;i<s1.length;i++){ list.add(s1[i]);

}

myAdapter=new MyAdapter();

gridView= (GridView) findViewById(R.id.gv);

gridView.setAdapter(myAdapter);

}

class MyAdapter extends BaseAdapter

{

@Override

public int getCount() {

return list.size();

}

@Override

public String getItem(int position) {

return list.get(position);

}

@Override public long getItemId(int position)

{

return position;

}

@Override

public View getView(int position,

View convertView,

ViewGroup parent)

{

 

LayoutInflater inflater=LayoutInflater.from(MainActivity.this);

View view=inflater.inflate(R.layout.my_girdview_item,null);

TextView tv= (TextView) view.findViewById(R.id.tv);

 tv.setText(getItem(position)); return view;

}

}

}

posted @ 2017-05-08 16:35  Hero/  阅读(472)  评论(0编辑  收藏  举报