Xamarin android grid并不是在添加背景图像时缩放单元格
问题描述:
我试图用8个不同的框(2 colums * 4 rows)进行网格布局。不幸的是,每当我尝试加载一个图像时,它都会出现bug和loades图像的放大版本。这是我的代码。必须注意的是工作得很好,当我刚刚加载一些写作Xamarin android grid并不是在添加背景图像时缩放单元格
首先,它看起来是这样的: Without background image
用下面的代码:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:background="@drawable/garage">
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@android:color/background_dark" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@android:color/background_light" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@android:color/holo_green_dark" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@android:color/holo_orange_dark" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@android:color/holo_purple" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@android:color/holo_orange_light" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@android:color/holo_red_light" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@android:color/darker_gray" />
</GridLayout>
我尝试添加背景图片后在细胞中的一个,这是我所得到的:Only one cell that takes up more space than the whole screen as it looks
具有以下代码:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:background="@drawable/garage">
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="@drawable/spaceshuttle1" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<TextView
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
</GridLayout>
我该怎么做,让图像留在它的细胞?
答
如果引用TextView.onMeasure:
基类实施措施默认为背景大小,除非大的尺寸是由MeasureSpec允许的。子类应该重写onMeasure(int,int)以提供更好的内容度量。
因此,如果您的背景图像相当大,TextView
将扩展到背景的大小。
要解决这个问题,你可以创建自己的TextView,并覆盖onMeasure
方法:
public class MyTextView:TextView
{
public MyTextView(Context c) : base(c)
{
}
public MyTextView(Context c, IAttributeSet attr) : base(c, attr)
{
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int parentWidth=MeasureSpec.GetSize(widthMeasureSpec);
int parentHeight = MeasureSpec.GetSize(heightMeasureSpec);
//set the size to the parent layout's cell's size.
this.SetMeasuredDimension(parentWidth/2, parentHeight/4);
}
}
而且在布局中使用MyTextView
:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="4">
<YourNameSpace.MyTextView
android:layout_column="1"
android:layout_row="1"
android:background="@drawable/beautifulchristmas"
/>
<TextView
android:layout_column="1"
android:layout_row="2"
android:background="@drawable/beautifulchristmas"/>
...