1. share_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:padding="30dp" > <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher"/> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:text="TextView"/> </RelativeLayout>

2.

share_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:padding="30dp" > <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher"/> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:text="TextView"/> </RelativeLayout>

3. ShareDialog.java

package com.zps.sharedemo;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; public class ShareDialog { private AlertDialog dialog;
private GridView gridView;
private RelativeLayout cancelButton;
private SimpleAdapter saImageItems;
private int[] image = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4 };
private String[] name = { "微博", "微信好友", "朋友圈", "QQ" }; public ShareDialog(Context context) { dialog = new AlertDialog.Builder(context).create();
dialog.show();
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM); // 非常重要:设置对话框弹出的位置
window.setContentView(R.layout.share_dialog);
gridView = (GridView) window.findViewById(R.id.share_gridView);
cancelButton = (RelativeLayout) window.findViewById(R.id.share_cancel);
List<HashMap<String, Object>> shareList = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < image.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", image[i]);// 添加图像资源的ID
map.put("ItemText", name[i]);// 按序号做ItemText
shareList.add(map);
} saImageItems = new SimpleAdapter(context, shareList,
R.layout.share_item, new String[] { "ItemImage", "ItemText" },
new int[] { R.id.imageView1, R.id.textView1 });
gridView.setAdapter(saImageItems);
} public void setCancelButtonOnClickListener(OnClickListener Listener) {
cancelButton.setOnClickListener(Listener);
} public void setOnItemClickListener(OnItemClickListener listener) {
gridView.setOnItemClickListener(listener);
} /**
* 关闭对话框
*/
public void dismiss() {
dialog.dismiss();
}
}

4.  MainActivity.java

package com.zps.sharedemo;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener
{ private Button shareButton;
ShareDialog shareDialog; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
shareButton = (Button) findViewById(R.id.shareButton);
shareButton.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.shareButton:
shareDialog = new ShareDialog(this);
shareDialog.setCancelButtonOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
shareDialog.dismiss();
}
});
shareDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
if (item.get("ItemText").equals("微博")) {
} else if (item.get("ItemText").equals("微信好友")) {
} else if (item.get("ItemText").equals("朋友圈")) {
} else if (item.get("ItemText").equals("QQ")) {
}
shareDialog.dismiss();
}
}); break; default:
break;
} }
}

5. activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/shareButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </RelativeLayout>

自定义ShareDialog视图的更多相关文章

  1. UICollectionView(集合视图)以及自定义集合视图

    一.UICollectionView集合视图           其继承自UIScrollView.         UICollectionView类是iOS6新引进的API,用于展示集合视图,布局 ...

  2. Android 自定义View修炼-自定义HorizontalScrollView视图实现仿ViewPager效果

    开发过程中,需要达到 HorizontalScrollView和ViewPager的效果,于是直接重写了HorizontalScrollView来达到实现ViewPager的效果. 实际效果图如下: ...

  3. 自定义View视图

    自定义View视图文件查找逻辑 之前MVC5和之前的版本中,我们要想对View文件的路径进行控制的话,则必须要对IViewEngine接口的FindPartialView或FindView方法进行重写 ...

  4. 自定义MVC视图引擎ViewEngine 创建Model的专属视图

    MVC内置的视图引擎有WebForm view engine和Razor view engine,当然也可以自定义视图引擎ViewEngine.本文想针对某个Model,自定义该Model的专属视图. ...

  5. Swift自定义头视图和尾视图

    var data: [[String]]! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup a ...

  6. ASP.NET MVC 自定义Razor视图WorkContext

    概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...

  7. Office365学习笔记—Xslt自定义列表视图

    1,在Office365中需要添加自定义的视图!用Spd添加视图,这儿我添加一个testView! (1)打开testView.aspx将</ZoneTemplate>节点中的内容全部删除 ...

  8. 解读ASP.NET 5 & MVC6系列(16):自定义View视图文件查找逻辑

    之前MVC5和之前的版本中,我们要想对View文件的路径进行控制的话,则必须要对IViewEngine接口的FindPartialView或FindView方法进行重写,所有的视图引擎都继承于该IVi ...

  9. sap表维护工具来维护自定义表&视图簇的使用

    一.通过表维护工具维护自定义表 1.SE11创建表 2.se11界面的菜单:实用程序->Table Maintenance Generator其实这里就是调用SE54 3.sm30 调用维护好的 ...

随机推荐

  1. [algothrim]URL相似度计算的思考

    http://www.spongeliu.com/399.html http://in.sdo.com/?p=865

  2. 2878: [Noi2012]迷失游乐园 - BZOJ

    Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环( ...

  3. Timer 的缺陷

    java.util.Timer计时器有管理任务延迟执行("如1000ms后执行任务")以及周期性执行("如每500ms执行一次该任务").但是,Timer存在一 ...

  4. Android工具包

    Eclipse + ADT +SDK,下载ADT Bundle全包含 adt-bundle-windows-x86_64-20140702 http://www.cnblogs.com/tc310/p ...

  5. JQuery绑定和注销事件

    $('#action_list > li').each(function(){ $(this).unbind('click') .bind('click', function(){ /** so ...

  6. PHP之set_error_handler()函数讲解

    定义和用法 set_error_handler() 函数设置用户自定义的错误处理函数. 该函数用于创建运行时期间的用户自己的错误处理方法. 该函数会返回旧的错误处理程序,若失败,则返回 null. 语 ...

  7. hbase操作的问题

    写了一个java程序,需要向hbase中写入大量的数据,但是这个程序执行一半就报错, 问题是,此时已经写入了很多数据. 查看jps,发现hmaster进程崩溃了. 基于以上信息,发现是在程序中,链接h ...

  8. python之self (转)

    首先明确的是self只有在类的方法中才会有,独立的函数或方法是不必带有self的.self在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数. self名称不是必须的,在python中self ...

  9. mysql的学习记录

    1 MySQL -h localhost -u UserName -p Password-h不写,默认为localhost注意:最好先MySQL -h localhost -u UserName -p ...

  10. 深入浅出Java并发包—锁机制(三)

    接上文<深入浅出Java并发包—锁机制(二)>  由锁衍生的下一个对象是条件变量,这个对象的存在很大程度上是为了解决Object.wait/notify/notifyAll难以使用的问题. ...