【问题标题】:Android (Xamarin) padding in gridview网格视图中的 Android (Xamarin) 填充
【发布时间】:2017-06-15 21:42:21
【问题描述】:

我有一些使用 gridview 和自定义适配器创建的按钮,但这些按钮被我认为是 gridview 一部分的填充包围。我找到了this thread 并尝试了解决方案,但无法解决问题。这是应用程序的图片:

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"
    };
}

任何帮助将不胜感激!

【问题讨论】:

  • 能否发布您的适配器代码和完整的 xml?
  • @Marc 按要求添加。
  • 很好奇为什么要重置布局参数和高度。尝试将它们放在 if 块中。将 button.Text = ... 留在原处。也尝试删除 button.SetHeight 行。
  • 我不相信这是一个填充。这似乎是所有 Android 按钮默认具有的“空间”,它是背景的一部分。要确认这一点,请将按钮背景更改为任何颜色:button.SetBackgroundColor(Android.Graphics.Color.Gray);
  • @apineda 谢谢!就是这样。您介意将此作为答案,以便我可以将问题标记为已回答吗?

标签: android xamarin layout


【解决方案1】:

我不相信这是一个填充。这似乎是所有 Android 按钮默认具有的“空间”,它是背景的一部分。

要确认这一点,请将按钮背景更改为任何颜色: button.SetBackgroundColor(Android.Graphics.Color.Gray);

【讨论】:

    【解决方案2】:

    在 Xamarin 中为 Layouts 提供填充是这样的:

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

    但 XML 不适用于 Xamarin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2014-11-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多