简单的方法来使一个ImageView的是一个GridView匹配下的GridView的大小到底是什么? [英] Easy way to make an ImageView that is underneath a GridView match the GridView's size exactly?
问题描述
我的的ImageView
显示的是一个国际象棋的风格板的图像的小游戏我正在做。它是由 GridView控件
显示我的作品的影像覆盖。我有它下面显示,但它是不正确地定位。我想它的 GridView控件
的完全匹配的边缘,但不知道怎么办。有一些方法,使的ImageView的
这样的话,我可以说match_parent中它的XML的高度和宽度等? GridView控件
父
My ImageView
is displaying an image of a chess style board for a little game I'm making. It is covered by a GridView
that displays the images of my pieces. I have it displaying underneath but it is not positioned properly. I want it to match exactly the edges of the GridView
but don't know how. Is there some way to make the GridView
a "parent" of the ImageView
so then I could say "match_parent" in the XML for it's height and width and so on?
下面是我的应用程序现在怎么看起来屏幕。我想丑有色网我做的 GridView控件
图像排队,使周围的我的作品边缘黑色方块在棋盘的正方形居中。
Here is a screen of how my app looks now. I want the ugly colored grid I made to line up with the GridView
images so that the black squares around the edge of my pieces are centered in the squares of the board.
下面是我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textFieldFU"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/gridview"
android:layout_alignTop="@+id/gridview"
android:layout_centerHorizontal="true" />
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="600dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="8"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
下面是相关的部分我的 MainActivity
:
Here is the relevant part of my MainActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageAdapter iA = new ImageAdapter(this);
final ImageView board;
board = (ImageView) findViewById(R.id.imageView1);
board.setImageResource(R.drawable.board1);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(iA);
...
这是我的 ImageAdapter
,我有它设置了 GridView控件
的相关部分:
And here is the relevant parts of my ImageAdapter
that I have which sets up the GridView
:
public class ImageAdapter extends BaseAdapter {
public static int[] images = { R.drawable.rock2, R.drawable.paper2,
R.drawable.scissors2, R.drawable.rock2, R.drawable.paper2,
...//This part goes on for a while...
...//On to the relevant part...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv;
if (convertView != null) {
iv = (ImageView) convertView;
} else {
iv = new ImageView(context);
iv.setLayoutParams(new GridView.LayoutParams(60, 60));
iv.setScaleType(ScaleType.CENTER);
iv.setPadding(0, 0, 0, 0);
}
iv.setImageResource(images[position]);
return iv;
}
public static int[] getImagesArray() {
return images;
}
public void setImagesArray(int[] newImages) {
images = newImages;
}
----------------编辑,添加了替代解决方案从下面.--------------答案
----------------EDIT, Added the alternative solution from answer below.--------------
我摆脱了的ImageView的
完全,只是添加到了我的 getView
在 ImageAdapter
类:
I got rid of the ImageView
entirely and just added this to my getView
in the ImageAdapter
class:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv;
if (convertView != null) {
iv = (ImageView) convertView;
} else {
iv = new ImageView(context);
iv.setLayoutParams(new GridView.LayoutParams(60, 60));
iv.setScaleType(ScaleType.CENTER);
iv.setPadding(0, 0, 0, 0);
if(position < 8 && position % 2 ==0){
iv.setBackgroundColor(Color.DKGRAY);
}
else if(position > 7 && position < 16 && position % 2 ==1){
iv.setBackgroundColor(Color.DKGRAY);
}
else if(position > 15 && position < 24 && position % 2 ==0){
iv.setBackgroundColor(Color.DKGRAY);
}
else if(position > 23 && position < 32 && position % 2 ==1){
iv.setBackgroundColor(Color.DKGRAY);
}
else if(position > 31 && position < 40 && position % 2 ==0){
iv.setBackgroundColor(Color.DKGRAY);
}
else if(position > 39 && position < 48 && position % 2 ==1){
iv.setBackgroundColor(Color.DKGRAY);
}
else if(position > 47 && position < 56 && position % 2 ==0){
iv.setBackgroundColor(Color.DKGRAY);
}
else if(position > 55 && position < 64 && position % 2 ==1){
iv.setBackgroundColor(Color.DKGRAY);
}
else
iv.setBackgroundColor(Color.GRAY);
}
iv.setImageResource(images[position]);
return iv;
}
下面是现在的样子:
推荐答案
您可以配置你的GridView的基本元素的布局。
与自定义背景的ImageView的好像是你所需要的。
你将不得不在哪一行你才能知道要使用的背景颜色getview检查。
You can configure the layout of the base element of your gridview. An imageView with a custom background seems to be what you need. You will have to check in getview in which row you are in order to know which background color to use.
这篇关于简单的方法来使一个ImageView的是一个GridView匹配下的GridView的大小到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!