我有这个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>
其中包含的图像如下所示:
如何使图像以统一的方式分布在屏幕上,即占据所有空间直到结束,以便两者具有相同的大小?
答案 0 :(得分:1)
将以下属性添加到所有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
进行操作。
答案 1 :(得分: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="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();
}
});
}
对于kotlin试试这个:
// 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