我尝试使用像 Image1 这样的网格视图来制作带有 4 个图像的主菜单活动。但是图片并没有占据整个Activity的空间。Image2 显示了我的网格视图。如何在不滚动的情况下对齐 4 个图像并像 Image1 一样适合所有屏幕?
活动主菜单.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
tools:context=".MainMenuActivity"
/>
MainMenuActivity.java
public class MainMenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView ;
if(convertView==null)
{
imageView = new ImageView(mcontext);
WindowManager wm = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = mcontext.getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
int width=(int) (dpWidth/2);
int height=(int) (dpHeight/2);
imageView.setLayoutParams(new GridView.LayoutParams(parent.getWidth()/2,parent.getHeight()/2));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setLayoutParams(new GridView.LayoutParams(width,height));
//imageView.setLayoutParams(new GridView.LayoutParams(imageView.getWidth(),imageView.getHeight()));
//imageView.setBackgroundColor(000000);
//imageView.setLayoutParams(new GridView.LayoutParams(400,200));
//imageView.setLayoutParams(new GridView.LayoutParams());
//imageView.setPadding(40,40,40,40);
}
else
{
imageView=(ImageView)convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.mainmenu1, R.drawable.mainmenu2,
R.drawable.mainmenu3, R.drawable.mainmenu4
};