我在网格视图中插入了图像和文本。 gridview单元格未正确对齐。我不明白为什么会这样。这是getview
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=null;
TextView textView = null;
View view;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MainActivity mainActivity=new MainActivity();
WindowManager wm = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int imgSize = 0;
if (convertView == null) {
// if it's not recycled, initialize some attributes
// grid = new View(mContext);
view=inflater.inflate(R.layout.grid_single,null);
textView = (TextView) view.findViewById(R.id.grid_text);
imageView = (ImageView)view.findViewById(R.id.grid_image);
imgSize=size.x;
// textView=new TextView(mContext);
// imageView = new ImageView(mContext);
// imageView.setLayoutParams(new GridView.LayoutParams(imgSize/2, imgSize/2));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageView.setPadding(0, 0, 0, 0);
// imageView = new ImageView(mContext);
textView.setText(menus[position]);
imageView.setImageResource(mThumbIds[position]);
} else {
// imageView = (ImageView) convertView;
view = convertView;
}
// textView=new TextView(mContext);
return view;
// return grid;
}
这是网格布局
<GridView
android:numColumns="2"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/grid"
/>
这是imageview和textview布局
<ImageView
android:id="@+id/grid_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
</ImageView>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="9sp" >
</TextView>
答案 0 :(得分:0)
更改网格项目布局,如下所示 -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/grid_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="9sp"
android:layout_below="@+id/grid_image"
android:layout_centerHorizontal="true" />
</RelativeLayout>