我想在我的 Galaxy Tab 上显示带有多个图像的网格视图...
我是开发新手,我将非常感谢您的帮助。
维韦克
public class MainActivity extends Activity {
GridView gv1;
GridViewAdapter madapter;
int arr[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String arr1[]={"ashu","hello","yes","no"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv1=(GridView)findViewById(R.id.gridView1);
madapter=new GridViewAdapter();
gv1.setAdapter(madapter);
}
适配器类:
class GridViewAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return arr1.length; }
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position; }
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0; }
@Override public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater infla=getLayoutInflater();
View v=infla.inflate(R.layout.main1,null);
TextView tv1=(TextView)v.findViewById(R.id.textView1);
ImageView iv1=(ImageView)v.findViewById(R.id.imageView1);
tv1.setText(arr1[position]);
iv1.setBackgroundResource(arr[position]);
return v; }}
}
activity_main.xml
<GridView
android:id="@+id/gridView1" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/> </RelativeLayout>
main1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:text="TextView" /> </RelativeLayout>