GridView的方形物品的机器人自适应宽/高 [英] GridView with square items in android with adaptive width/height
问题描述
我有一个自定义的GridView中包含的项目
I have a custom gridview with items like that
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:longClickable="false"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:longClickable="false"
android:text="0"
android:textSize="60sp" />
</LinearLayout>
我希望我的项目是正方形,我想GridView的伸展宽度,以填补屏幕的纵向所有所有的宽度和高度都在横向方向。 它应该是这样的
I want my items to be squares and I want gridview to stretch width to fill all all width of screen in portrait orientation and all height in landscape orientation. It should look like this
其中A - 是一个正方形的边和B是边缘部宽度(它可以为零)。 我想,我也许应该重写onMeasure方法,但究竟我应该怎么办? 也许有人可以帮助?
Where A - is the side of a square and B is the margin width (it could be zero). I think that I should probably override onMeasure method, but what exactly should I do? Maybe anyone can help?
编辑 好吧,我试图手动适配器的getView方法,最好设置宽度项目和高度,但它仍然不是我想要的。我怎样才能列之间摆脱那个间距?
EDIT OK, I tried to set width and height of items manually in getView method of adapter, it's better, but still it's not what I wanted. How can I get rid of that spacing between columns?
推荐答案
首先,你要创建一个自定义视图,你可以使用默认的,而不是LinearLayout中你使用类。然后,你想覆盖查看的onMeasure电话,并迫使它是方:
First, you're want to create a custom View class that you can use instead of the default LinearLayout you're using. Then you want to override the View's onMeasure call, and force it to be square:
public class GridViewItem extends ImageView {
public GridViewItem(Context context) {
super(context);
}
public GridViewItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridViewItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec); // This is the key that will make the height equivalent to its width
}
}
然后你就可以改变你的row_grid.xml文件:
Then you can change your row_grid.xml file to:
<path.to.item.GridViewItem xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher" >
</path.to.item.GridViewItem>
只是一定要改变path.to.item到你GridViewItem.java类所在的包。
Just be sure to change "path.to.item" to the package where your GridViewItem.java class resides.
编辑:
也改变scaleType从fitXY到centerCrop使图像不拉伸本身并保持其纵横比。而且,只要它是一个正方形图像,没有什么应该被裁剪无关。
Also changed scaleType from fitXY to centerCrop so that your image doesn't stretch itself and maintains its aspect ratio. And, as long as it's a square image, nothing should be cropped, regardless.
这篇关于GridView的方形物品的机器人自适应宽/高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!