如何在Android中创建自定义共享对话框 [英] How to create custom share dialog in Android

查看:102
本文介绍了如何在Android中创建自定义共享对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从应用程序到社交媒体使用共享选项. Android默认共享对话框可以正常工作.我想自定义对话框,以便可以像Flipboard android应用程序中一样重新排列共享对话框UI.谁能指出该怎么做?

I am using share option from my application to social media. Android default share dialog works fine. I want to customize the dialog so that I can rearrange the share dialog UI similarly as in Flipboard android app. Can any one point out how to do that?

推荐答案

我遇到了这个问题.我在答案中找到了解决方案,希望对您也有帮助.

i faced this problem. i found solution in this answer i hope it helps you too..

如果以后不存在,我会在下面写这篇文章的代码:

I write the code of this post below in case of not exist in the future:

您必须使用自定义ListAdapter来添加图像.即将继承的是ArrayAdapter的子类(默认情况下由AlertDialog使用).这是一个示例:

You must use a custom ListAdapter to add your image. On way is to subclass the ArrayAdapter (used by default by the AlertDialog). Here is an example:

final Item[] items = {
    new Item("Email", android.R.drawable.ic_menu_add),
    new Item("Facebook", android.R.drawable.ic_menu_delete),
    new Item("...", 0),//no icon for this one
};

ListAdapter adapter = new ArrayAdapter<Item>(
    this,
    android.R.layout.select_dialog_item,
    android.R.id.text1,
    items){
        public View getView(int position, View convertView, ViewGroup parent) {
            //Use super class to create the View
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView)v.findViewById(android.R.id.text1);

            //Put the image on the TextView
            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);

            //Add margin between image and text (support various screen densities)
            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            tv.setCompoundDrawablePadding(dp5);

            return v;
        }
    };


new AlertDialog.Builder(this)
    .setTitle("Share Appliction")
    .setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            //...
        }
    }).show();

这是Item类

public static class Item{
    public final String text;
    public final int icon;
    public Item(String text, Integer icon) {
        this.text = text;
        this.icon = icon;
    }
    @Override
    public String toString() {
        return text;
    }
}

这篇关于如何在Android中创建自定义共享对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆