你的位置:首页 > 操作系统

[操作系统]自定义ShareDialog视图


Screenshot_2015-12-08-13-17-56

1. share_item.

<??><RelativeLayout ="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.

<??><RelativeLayout ="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.

<RelativeLayout ="http://schemas.android.com/apk/res/android"  ="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>