2017-06-15 70 views
4

我有一些使用gridview和自定義適配器創建的按鈕,但按鈕被填充,我相信是gridview的一部分。我發現this thread並嘗試瞭解決方案,但無法解決問題。下面是應用程序的圖片:gridview中的Android(Xamarin)填充

enter image description here

XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="1" 
    android:orientation="vertical" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:background="@drawable/background"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.4" /> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.6"> 
     <GridView 
      android:id="@+id/mainMenuGridview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:numColumns="2" 
      android:listSelector="@null" 
      android:verticalSpacing="0dp" 
      android:horizontalSpacing="0dp" /> 
    </LinearLayout> 
</LinearLayout> 

適配器代碼:

class CustomAdapter : BaseAdapter 
{ 
    private readonly Context context; 
    private int numRows = 2; 

    public CustomAdapter(Context c) 
    { 
     context = c; 
    } 

    public override int Count 
    { 
     get { return buttonTexts.Length; } 
    } 

    public override Java.Lang.Object GetItem(int position) 
    { 
     return null; 
    } 

    public override long GetItemId(int position) 
    { 
     return 0; 
    } 

    public override View GetView(int position, View convertView, ViewGroup parent) 
    { 
     Button button; 

     if (convertView == null) 
     { 
      // if it's not recycled, initialize some attributes 
      button = new Button(context); 
     } 
     else 
     { 
      button = (Button)convertView; 
     } 

     button.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent); 

     button.SetHeight(Convert.ToInt32((Main_Menu.SCREEN_HEIGHT - Main_Menu.ACTIONBAR_HEIGHT) * 0.6)/numRows); 
     button.Text = buttonTexts[position]; 
     return button; 
    } 

    private readonly string[] buttonTexts = 
    { 
     "Button 1", 
     "Button 2", 
     "Button 3", 
     "Button 4" 
    }; 
} 

任何幫助將不勝感激!

+0

您可以發佈您的適配器代碼和完整的XML,好嗎? – Marc

+0

@Marc按要求添加。 – Ryalis

+0

好奇你爲什麼重置佈局參數和高度。嘗試把它們放在if塊中。離開button.Text = ...它在哪裏。也請嘗試刪除button.SetHeight行。 – Marc

回答

2

我不相信這是一個空白。這似乎是所有Android按鈕默認使用的「空間」,它是背景的一部分。

爲了證實這個按鈕背景更改爲任何顏色: button.SetBackgroundColor(Android.Graphics.Color.Gray);

0
在Xamarin

給填充到佈局是這樣的:

<StackLayout Padding="0,20,0,0"></StackLayout> 

但XML不是Xamarin