2017-06-15 72 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