具有统一列大小的 GridView
GridView with uniform column size
我有这个 GridLayout:
<!--back-->
<GridLayout
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="5">
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
</GridLayout>
其中包含的图像如下所示:
如何让图片在屏幕上均匀分布,即占据所有space直到最后,使两者大小相同?
将以下属性添加到所有 imageView
s
app:layout_columnWeight="1"
app:layout_rowWeight="1"
例如:
<GridLayout
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="5">
<ImageView
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:src="@drawable/noTexture"/>
..............
对所有 imageView
执行上述操作。
您必须按如下方式设置每个框的限制:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
java代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(HelloGridView.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
对于科特林,试试这个:
// references to our images
private val mThumbIds = arrayOf<Int>(
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)
class ImageAdapter(private val mContext: Context) : BaseAdapter() {
override fun getCount(): Int = mThumbIds.size
override fun getItem(position: Int): Any? = null
override fun getItemId(position: Int): Long = 0L
// create a new ImageView for each item referenced by the Adapter
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val imageView: ImageView
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = ImageView(mContext)
imageView.layoutParams = ViewGroup.LayoutParams(85, 85)
imageView.scaleType = ImageView.ScaleType.CENTER_CROP
imageView.setPadding(8, 8, 8, 8)
} else {
imageView = convertView as ImageView
}
imageView.setImageResource(mThumbIds[position])
return imageView
}
}
了解有关 GridView 的更多信息:Click this
我有这个 GridLayout:
<!--back-->
<GridLayout
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="5">
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
<ImageView
android:src="@drawable/noTexture"/>
</GridLayout>
其中包含的图像如下所示:
如何让图片在屏幕上均匀分布,即占据所有space直到最后,使两者大小相同?
将以下属性添加到所有 imageView
s
app:layout_columnWeight="1"
app:layout_rowWeight="1"
例如:
<GridLayout
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="5">
<ImageView
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:src="@drawable/noTexture"/>
..............
对所有 imageView
执行上述操作。
您必须按如下方式设置每个框的限制:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
java代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(HelloGridView.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
对于科特林,试试这个:
// references to our images
private val mThumbIds = arrayOf<Int>(
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)
class ImageAdapter(private val mContext: Context) : BaseAdapter() {
override fun getCount(): Int = mThumbIds.size
override fun getItem(position: Int): Any? = null
override fun getItemId(position: Int): Long = 0L
// create a new ImageView for each item referenced by the Adapter
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val imageView: ImageView
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = ImageView(mContext)
imageView.layoutParams = ViewGroup.LayoutParams(85, 85)
imageView.scaleType = ImageView.ScaleType.CENTER_CROP
imageView.setPadding(8, 8, 8, 8)
} else {
imageView = convertView as ImageView
}
imageView.setImageResource(mThumbIds[position])
return imageView
}
}
了解有关 GridView 的更多信息:Click this