我想用三行和多列实现gridview。我尝试了以下代码,但无法实现我想要的目标。
我的xml文件是
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:fillViewport="true" >
<GridView
android:id="@+id/gridView"
android:layout_width="340dp"
android:layout_height="385dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:padding="5dp"
android:scrollbars="horizontal">
</GridView>
</HorizontalScrollView>
</LinearLayout>
和活动文件代码是
gridView.setAdapter(new ImageAdapter(this,id_list,favflag_list,thumb_img_list));
gridView.setNumColumns(5);
是可以实现的还是其他任何方式
答案 0 :(得分:1)
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
绑定适配器:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14,
R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
主要活动:
public class AndroidGridLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
}
答案 1 :(得分:0)
取决于您的数据。如果您添加更多数据,它将改变其大小。例。如果你有9张图片存储在gridview中。你必须在xml中设置android:numColumns = "3"
,它会显示3行。
答案 2 :(得分:-1)
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
自定义xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imagepart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textpart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
主要活动:
public class CustomGridView extends Activity {
// references to our images
private Integer[] mThumbIds = {
R.drawable.androider_01,
R.drawable.androider_02,
R.drawable.androider_03,
R.drawable.androider_04,
R.drawable.androider_05,
R.drawable.androider_06,
R.drawable.androider_07,
R.drawable.androider_08,
R.drawable.androider_09,
R.drawable.androider_10,
R.drawable.androider_11,
R.drawable.androider_12,
R.drawable.androider_13,
R.drawable.androider_14,
R.drawable.androider_15,
R.drawable.androider_16
};
public class MyAdapter extends BaseAdapter {
private Context mContext;
public MyAdapter(Context c) {
// TODO Auto-generated constructor stub
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mThumbIds[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
if(convertView==null){
grid = new View(mContext);
LayoutInflater inflater=getLayoutInflater();
grid=inflater.inflate(R.layout.mygrid, parent, false);
}else{
grid = (View)convertView;
}
ImageView imageView = (ImageView)grid.findViewById(R.id.imagepart);
TextView textView = (TextView)grid.findViewById(R.id.textpart);
imageView.setImageResource(mThumbIds[position]);
textView.setText(String.valueOf(position));
return grid;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MyAdapter(this));
}
}