安卓学习UI组件-GridView-常用属性与自定义适配器,图文排列

一.常用属性与自定义适配器。网格方式排列视图

安卓学习UI组件-GridView-常用属性与自定义适配器,图文排列_第1张图片

图片应该设置大小一样

MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {

private GridView gridView;
/*GridView组件:
*1.自定义适配器
* (1)创建一个类,继承BaseAdapter类
* (2)实现四个方法
* getCount:获取要显示的选项总数
* getItem:获取每一个选项
* getItemId:选项的ID值
* getView:该方法用来为每一个选项生成视图(ImageView),该方法会被多次调用
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView)findViewById(R.id.gridView);
//绑定适配器
gridView.setAdapter(new MyAdapter(this));
}


//自定义适配器
static class MyAdapter extends BaseAdapter
{
//图片大小要一样
private int[] images={R.mipmap.pic0,
R.mipmap.pic1,
R.mipmap.pic2,
R.mipmap.pic3,
R.mipmap.pic4,
R.mipmap.pic5,
R.mipmap.pic6,
R.mipmap.pic7,
R.mipmap.pic8,
R.mipmap.pic9,
R.mipmap.pic10,
R.mipmap.pic11,
R.mipmap.pic12,
R.mipmap.pic13,
R.mipmap.pic14,
R.mipmap.pic15};
private Context context;
public MyAdapter(Context context)
{
this.context=context;
}
@Override
public int getCount() {
return images.length;
}

@Override
public Object getItem(int position) {
return images[position];
}

@Override
public long getItemId(int position) {
return position;
}
//关键 重复调用给每一个ITEM 填充
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv=new ImageView(context);

iv.setImageResource(images[position]);
return iv;
}
}
}
<?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="com.example.administrator.myapplication.MainActivity">

<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>

 

二.带图片和文本视图的GridView

安卓学习UI组件-GridView-常用属性与自定义适配器,图文排列_第2张图片

ManActivity.java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity2 extends Activity {
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
gridView= (GridView) findViewById(R.id.gridView2);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("parent="+parent);
System.out.println("view="+view);//每一个选项的布局
System.out.println("position="+position);//位置
System.out.println("id=" + id);///id
TextView tv=(TextView)view.findViewById(R.id.textView);
Toast.makeText(MainActivity2.this,tv.getText(),Toast.LENGTH_SHORT).show();
}
});
}

//静态类相当于一个外部类 不依赖于外部类的对象 比非静态内部类效率更高
static class MyAdapter extends BaseAdapter{

private String[] names={"转账","查询","金融","基金","国债","信用卡","商城","充值","红包"};
private int[] images={R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher
};


private Context context;
public MyAdapter(Context context)
{
this.context=context;
}

@Override
public int getCount() {
return names.length;
}

@Override
public Object getItem(int position) {
return names[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
View view=inflater.inflate(R.layout.item, null);
ImageView iv= (ImageView) view.findViewById(R.id.imageView);
TextView tv= (TextView) view.findViewById(R.id.textView);
iv.setImageResource(images[position]);
tv.setText(names[position]);


return view;
}
}
}

activity_main.xml

<?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="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.example.administrator.myapplication.MainActivity2">

<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView2"
android:numColumns="4"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="New Text"
android:id="@+id/textView" />
</LinearLayout>
 

你可能感兴趣的:(安卓学习UI组件-GridView-常用属性与自定义适配器,图文排列)