0

我正在尝试添加带有自定义按钮的自定义对话框,这可能吗?如果是这样,我们该怎么做?

在此处输入图像描述

4

1 回答 1

2

这当然是可能的。看看GridView你需要创建一个自定义Adapter来为网格中的每个项目加载图像和标题。

我会这样做。

创建一个简单的类来保存有关网格中每个项目的信息。让我们称之为GridItem

public class GridItem
{
    public string Title { get; set; }
    public int ImageResourceId { get; set; }
    public Action ClickAction { get; set; }
}

Title是图像下的标题,ImageResoruceId是项目中的可绘制资源 ID,即:Resource.Drawable.googleplus并且ClickActionAction单击项目时要做的事情。

现在为 Grid 中的项目创建一个布局。grid_item.axml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/grid_item_title"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:layout_centerHorizontal="true" />
    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_above="@+id/grid_item_title"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" />
</RelativeLayout>

现在一个习惯AdapterGridAdapter.

public class GridAdapter : BaseAdapter<GridItem>
{
    private readonly IList<GridItem> _items;
    private readonly Context _context;

    public GridAdapter(Context context, IList<GridItem> items)
    {
        _context = context;
        _items = items;
    }

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

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = _items[position];
        var view = convertView;
        if (view == null)
        {
            var inflater = LayoutInflater.FromContext(_context);
            view = inflater.Inflate(Resource.Layout.grid_item, parent, false);
            view.Clickable = true;
            view.Click += (s, a) => item.ClickAction.Invoke();
        }

        var image = view.FindViewById<ImageView>(Resource.Id.grid_item_image);
        var title = view.FindViewById<TextView>(Resource.Id.grid_item_title);

        title.Text = item.Title;
        image.SetImageResource(item.ImageResourceId);

        return view;
    }

    public override int Count
    {
        get { return _items.Count; }
    }

    public override GridItem this[int position]
    {
        get { return _items[position]; }
    }
}

GridView以及自身的布局, CustomGridViewDialog.axml.

<GridView
  android:id="@+id/gridview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:numColumns="2"
  android:verticalSpacing="10dp"
  android:horizontalSpacing="10dp"
  android:stretchMode="columnWidth"
  android:gravity="center"/>

然后,在创建您的代码时,Dialog您将拥有类似这样的代码:

var customView = LayoutInflater.Inflate (Resource.Layout.CustomGridViewDialog, null);
var items = new List<GridItem>
{
    new GridItem
    {
        Title = "Google",
        ItemResourceId = Resource.Drawable.google,
        ClickAction = () => { Google(); }
    },
    ...
};
(customView.FindViewById<GridView>(Resource.Id.gridview)).Adapter = new GridAdapter(this, items);

var builder = new AlertDialog.Builder(this);
builder.SetTitle("Share");
builder.SetView(customView);

应该就是这样。

于 2013-07-23T22:04:35.737 回答