我正在使用GridView来显示图像(仅限平板电脑),但是在搜索到的图像之间获得了太多空间但仍然没有得到解决方案,找到下面的xml文件,java代码和屏幕截图哪里出错了。 我的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="@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.naveenbannikoppa.grid.AndroidGridViewDisplayImages" >
<GridView
android:id="@+id/gridview_android_example"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:layout_weight="100"
android:horizontalSpacing="0dip"
android:verticalSpacing="0dip"
android:clipChildren="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:numColumns="3" >
</GridView>
</RelativeLayout>
Java文件
public class AndroidGridViewDisplayImages extends AppCompatActivity {
GridView androidGridView;
Integer[] imageIDs = {
R.drawable.mobile_app_41, R.drawable.mobile_app_41, R.drawable.mobile_app_42,
R.drawable.mobile_app_43, R.drawable.mobile_app_46, R.drawable.mobile_app_46,
R.drawable.mobile_app_46, R.drawable.mobile_app_41, R.drawable.mobile_app_42,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview_android_example_with_image);
androidGridView = (GridView) findViewById(R.id.gridview_android_example);
androidGridView.setAdapter(new ImageAdapterGridView(this));
androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id) {
Toast.makeText(getBaseContext(), "Grid Item " + (position + 1) + " Selected", Toast.LENGTH_LONG).show();
}
});
}
public class ImageAdapterGridView extends BaseAdapter {
private Context mContext;
public ImageAdapterGridView(Context c) {
mContext = c;
}
public int getCount() {
return imageIDs.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView mImageView;
if (convertView == null) {
mImageView = new ImageView(mContext);
mImageView.setLayoutParams(new GridView.LayoutParams(150, 150));
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setAdjustViewBounds(true);
mImageView.setPadding(8, 8, 8, 8);
} else {
mImageView = (ImageView) convertView;
}
mImageView.setImageResource(imageIDs[position]);
return mImageView;
}
}
}
屏幕截图
我想要这样
答案 0 :(得分: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:columnWidth="100dp"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
答案 1 :(得分:0)
使用此代码,增加垂直间距......
<?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:columnWidth="100dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
答案 2 :(得分:0)
你可以在GridView标签中使用 android:verticalSpacing 和 android:horizontalSpacing
android:horizontalSpacing =&#34; 10dp&#34; //两个项目之间的空格(水平)
android:verticalSpacing =&#34; 10dp&#34; //两行之间的空格(垂直)
答案 3 :(得分:0)
如果你总想在一行中有3列,你应该计算每个项目的高度取决于屏幕高度(它将等于li
)
screenheight-gap/numberofitem
并在您的xml中,将if (convertView == null) {
int screenHeight = getResources().getDisplayMetrics().heightPixels;
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int height = MIN(screenHeight,screenWidth);
int numberOfCollumns = 3;
int verticalSpace = 10;
int itemHeight = (height - (numberOfCollumns + 1) * horizontalSpace) / numberOfCollumns;
mImageView = new ImageView(mContext);
//mImageView.setLayoutParams(new GridView.LayoutParams(150, 150));
mImageView.setLayoutParams(new GridView.LayoutParams(itemHeight, itemHeight));
...
}
GridView
更改为android:layout_width="fill_parent"
android:layout_width="wrap_content"
答案 4 :(得分:0)
实际上你的布局没有问题。问题是您使用的图像大小。在改变图像大小后尝试。