gpt4 book ai didi

java - Android 的 gridview 中的水平滚动和不同尺寸的图像,如 Windows8 App Tiles

转载 作者:行者123 更新时间:2023-12-01 04:26:35 25 4
gpt4 key购买 nike

我也尝试过 android:scrollbars="horizo​​ntal" 属性,但当我将列数从 4 增加到 10 时它不起作用,它不显示滚动条,它会打乱它们彼此重叠的图像。无论如何,是否可以在水平而不是垂直中滑动图像,以及如何在网格中使用多种尺寸的图像,如下所示

enter image description here

当我设置第4列时,它看起来像这样 enter image description here

当我将列号增加到列号 10

enter image description here

activity_main.xml代码:

<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="150dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"

android:gravity="center"
android:background="@drawable/background"
android:scrollbars="horizontal"


/>

MainActivity.java

package com.example.hellogridview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import android.view.View;


public class MainActivity extends Activity {

@Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// }

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
//gridview.setNumColumns(10);

gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

ImageAdapter.java 类

package com.example.hellogridview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}

imageView.setImageResource(mThumbIds[position]);
return imageView;
}

// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}

我想要什么:

1) 我想水平滑动图像2) 图像应该具有不同的尺寸,如 Windows8 MetroStyle,如上面的第一张图像

最佳答案

Gridview 不适用于不同大小的单元格。您必须为该外观自定义图 block 布局。为此,您可以使用 tableLayout 或 LinearLayout。

关于java - Android 的 gridview 中的水平滚动和不同尺寸的图像,如 Windows8 App Tiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18444123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com