Android 中自定义分享样式弹窗

转自:http://blog.csdn.net/Beyond0525/article/details/46661147


Android 中自定义分享样式弹窗

开发过程中有很多弹窗需求,比如分享时弹窗,如下:

Android 中自定义分享样式弹窗_第1张图片

这其中就需要定义弹窗样式,位置等属性,要实现上述效果,可以通过两种比较常见的方式实现

1. 自定义dialog view 指定相关属性

2. 用dialog activity 去实现

上面实现的效果都会一样,总的来说就是要 一、自定义好view  二、控制弹出的位置

下面用dialog自定义的方式实现:

(1)定义view 的xml

[html]  view plain  copy
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@color/white"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginTop="@dimen/common_margin_default1"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <RelativeLayout  
  15.             android:id="@+id/view_share_pengyou"  
  16.             android:layout_width="0dp"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_weight="1" >  
  19.   
  20.             <ImageView  
  21.                 android:id="@+id/share_icon"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content"  
  24.                 android:layout_centerHorizontal="true"  
  25.                 android:src="@drawable/bg_share_pengyou" />  
  26.   
  27.             <TextView  
  28.                 android:id="@+id/share_name"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:layout_below="@id/share_icon"  
  32.                 android:layout_centerHorizontal="true"  
  33.                 android:layout_marginTop="@dimen/common_margin_small"  
  34.                 android:text="@string/share_to_pengyou"  
  35.                 android:textColor="@color/common_font_color_5"  
  36.                 android:textSize="@dimen/common_font_size_6" />  
  37.         RelativeLayout>  
  38.   
  39.         <RelativeLayout  
  40.             android:id="@+id/view_share_weixin"  
  41.             android:layout_width="0dp"  
  42.             android:layout_height="wrap_content"  
  43.             android:layout_weight="1" >  
  44.   
  45.             <ImageView  
  46.                 android:id="@+id/share_icon"  
  47.                 android:layout_width="wrap_content"  
  48.                 android:layout_height="wrap_content"  
  49.                 android:layout_centerHorizontal="true"  
  50.                 android:src="@drawable/bg_share_weixin" />  
  51.   
  52.             <TextView  
  53.                 android:id="@+id/share_name"  
  54.                 android:layout_width="wrap_content"  
  55.                 android:layout_height="wrap_content"  
  56.                 android:layout_below="@id/share_icon"  
  57.                 android:layout_centerHorizontal="true"  
  58.                 android:layout_marginTop="@dimen/common_margin_small"  
  59.                 android:text="@string/share_to_weixin"  
  60.                 android:textColor="@color/common_font_color_5"  
  61.                 android:textSize="@dimen/common_font_size_6" />  
  62.         RelativeLayout>  
  63.     LinearLayout>  
  64.   
  65.     <View  
  66.         android:layout_width="fill_parent"  
  67.         android:layout_height="@dimen/common_divider_height"  
  68.         android:layout_marginTop="@dimen/common_margin_default1"  
  69.         android:background="@color/common_line_color_2" />  
  70.   
  71.     <Button  
  72.         android:id="@+id/share_cancel_btn"  
  73.         android:layout_width="fill_parent"  
  74.         android:layout_height="wrap_content"  
  75.         android:text="@string/share_to_cancel" />  
  76.   
  77. LinearLayout>  

(2)在dialog中inflater 自定义view

[java]  view plain  copy
  1. private void showShareDialog() {  
  2.       L.d(TAG, "showShareDialog");  
  3.   
  4.       View view = LayoutInflater.from(mContext).inflate(R.layout.layout_share_weixin_view, null);  
  5.       // 设置style 控制默认dialog带来的边距问题  
  6.       final Dialog dialog = new Dialog(this, R.style.common_dialog);  
  7.       dialog.setContentView(view);  
  8.       dialog.show();  
  9.   
  10.       // 监听  
  11.       View.OnClickListener listener = new OnClickListener() {  
  12.   
  13.           @Override  
  14.           public void onClick(View v) {  
  15.               
  16.               switch (v.getId()) {  
  17.   
  18.                   case R.id.view_share_weixin:  
  19.                       // 分享到微信  
  20.                       onShare2Weixin();  
  21.                       break;  
  22.   
  23.                   case R.id.view_share_pengyou:  
  24.                       // 分享到朋友圈  
  25.                       onShare2Weixin();  
  26.                       break;  
  27.   
  28.                   case R.id.share_cancel_btn:  
  29.                       // 取消  
  30.                       break;  
  31.   
  32.               }  
  33.   
  34.               dialog.dismiss();  
  35.           }  
  36.   
  37.       };  
  38.       ViewGroup mViewWeixin = (ViewGroup) view.findViewById(R.id.view_share_weixin);  
  39.       ViewGroup mViewPengyou = (ViewGroup) view.findViewById(R.id.view_share_pengyou);  
  40.       Button mBtnCancel = (Button) view.findViewById(R.id.share_cancel_btn);  
  41.       mBtnCancel.setTextColor(R.color.common_font_color_2);  
  42.       mViewWeixin.setOnClickListener(listener);  
  43.       mViewPengyou.setOnClickListener(listener);  
  44.       mBtnCancel.setOnClickListener(listener);  
  45.   
  46.       // 设置相关位置,一定要在 show()之后  
  47.       Window window = dialog.getWindow();  
  48.       window.getDecorView().setPadding(0000);  
  49.       WindowManager.LayoutParams params = window.getAttributes();  
  50.       params.width = LayoutParams.MATCH_PARENT;  
  51.       params.gravity = Gravity.BOTTOM;  
  52.       window.setAttributes(params);  
  53.   
  54.   }  

上面做了一个简易的实现,效果和上面差不多,如下:

Android 中自定义分享样式弹窗_第2张图片


遇到的问题:

1. 如何定义dialog的弹窗位置:

Android 中自定义分享样式弹窗_第3张图片


2.实现底部对齐充满屏幕属性后发现会有边距,这时候需要重新定义dialog style,默认情况下的style中是带有padiing的

[html]  view plain  copy
  1.   
  2.    <style name="common_dialog" parent="@android:style/Theme.Dialog">  
  3.        <item name="android:windowBackground">@android:color/transparentitem>  
  4.        <item name="android:windowNoTitle">trueitem>  
  5.    style>  

然后在创建Dialog的时候指定theme就行。


即可完美实现。


你可能感兴趣的:(android)