【问题标题】:How to create custom share dialog in Android如何在 Android 中创建自定义共享对话框
【发布时间】:2012-12-10 19:07:49
【问题描述】:

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

【问题讨论】:

  • 看这个例子可能对你有帮助clickclickclack.wordpress.com/2012/01/03/…
  • 这是一个很好的例子。我会试试的。
  • 示例正在运行,但您需要对其进行一些更改
  • 就像你需要为列表视图行创建一个 R.layout.basiclistview xml。
  • @intrepidkarthi 你完成了自定义共享对话框

标签: android share android-dialog


【解决方案1】:

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

我把这篇文章的代码写在下面,以防将来不存在:

您必须使用自定义 ListAdapter 来添加您的图像。方法是继承 ArrayAdapter(AlertDialog 默认使用)。这是一个例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多