2017-03-07 33 views
1

我試圖用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> 

我該怎麼做,讓圖像留在它的細胞?

回答

1

如果引用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"/> 
    ...