我正在制作一个简单的壁纸应用程序,我有一些非常高分辨率的图像。我想知道是否有可能制作单列gridview,其中每个图像100%适合设备的宽度。我想要的一个例子如下图所示。
这是我的xml代码:
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
>
</GridView>
这是我的图片的java代码:
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
final int p = position;
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(2, 2, 2, 2);
} else {
imageView = (ImageView) convertView;
}
答案 0 :(得分:2)
您可以将numCols
设置为1。
android:numColumns="1"
in
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
>
</GridView>
您也可以参考Gallery-like Single column Gridview。希望这会有所帮助。
答案 1 :(得分:1)
尝试这样的事情:
<GridView
android:id="@+id/grdProducts"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
Java文件:
private GridView gridView;
private int columnWidth;
public static final int NUM_OF_COLUMNS = 1;
public static final int GRID_PADDING = 5;
gridView = (GridView)findViewById(R.id.grdProducts);
InitilizeGridLayout();
和Initialize GridLayout()
private void InitilizeGridLayout() {
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) (((getScreenWidth()) - ((NUM_OF_COLUMNS + 1) * padding)) / NUM_OF_COLUMNS);
gridView.setNumColumns(NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding,(int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
@SuppressLint("NewApi")
public int getScreenWidth() {
int columnWidth ;
WindowManager wm = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
columnWidth = point.x;
return columnWidth;
}