为了方便以后开发和熟悉代码,小手亲自实现分享界面。为了减少过多的相关文件,界面使用代码实现,实现如下:
package cn.zszh.customs.widget;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.Toast;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.favorite.WechatFavorite;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.zszh.customs.view.TextView;
import cn.zszh.customs.widget.base.BaseDialog;
import com.example.zszhtest.R;
public class ShareDialog extends BaseDialog{
//界面显示的数据
private String[] mTexts={"QQ好友","QQ空间","微信好友","微信朋友圈","微信收藏","新浪微博"};
private int[] mImgs={R.drawable.ic_share_qq,R.drawable.ic_share_qq_space,
R.drawable.ic_share_wx,R.drawable.ic_share_wx_friends,R.drawable.ic_share_wx_collection,
R.drawable.ic_share_xl};//图片数据
private OnClickListener mListener;
public ShareDialog(Context context) {
super(context);
mCreateView=initView();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置Dialog没有标题。需在setContentView之前设置,在之后设置会报错
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置Dialog背景透明效果,必须设置一个背景,否则会有系统的Dialog样式:外部白框
this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
setContentView(mCreateView);//添加视图布局
setLayout();
}
private View initView() {
int num=mTexts.length;
int number=3;//一行显示的数量 //最外面的布局
LinearLayout group=new LinearLayout(mContext);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
group.setLayoutParams(params);
group.setBackgroundColor(Color.BLACK);
group.setOrientation(LinearLayout.VERTICAL);//设置垂直方向
//标题
TextView title=new TextView(mContext);
params=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
50*mDensity);
params.bottomMargin=1;
title.setLayoutParams(params);
title.setText("分享");
title.setTextColor(Color.BLACK);
title.setTextSize(16);
title.setGravity(Gravity.CENTER);//字居中
title.setBackgroundColor(Color.WHITE);
group.addView(title);
//设置内容
for(int i=0,j=0;i
package cn.zszh.customs.widget.base;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
public class BaseDialog extends Dialog{
public View mCreateView;
public Context mContext;
public int mScreenWidth;//屏幕宽
public int mScreenHeight;//屏幕高
public int mDensity;//单位像素
public Animation mExitAnim;//退出动画
public Animation mEnterAnim;//进入动画
public BaseDialog(Context context) {
super(context);
init(context);
}
private void init(Context context) {
mContext=context;
//计算屏幕的宽高像素
DisplayMetrics metrics=mContext.getResources().getDisplayMetrics();
mScreenHeight=metrics.heightPixels;
mScreenWidth=metrics.widthPixels;
mDensity=(int) metrics.density;
}
@Override
public void show() {
super.show();
enterAnimation();//进入动画
}
/**
* 进入动画
*/
private void enterAnimation(){
if(mEnterAnim==null){
mEnterAnim=new TranslateAnimation(1, 0, 1, 0, 1, 1, 1, 0);
mEnterAnim.setDuration(500);
}
mCreateView.startAnimation(mEnterAnim);
}
/**
* 退出动画
*/
private void exitAnimation(){
if(mExitAnim==null){
mExitAnim=new TranslateAnimation(1, 0, 1, 0, 1, 0, 1, 1);
mExitAnim.setDuration(500);
mExitAnim.setAnimationListener(
new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
dismissDialog(); //动画完成执行关闭
}
});
}
mCreateView.startAnimation(mExitAnim);
}
/**
* 执行关闭
*/
private void dismissDialog() {
super.dismiss();
}
/**
* 执行动画
*/
@Override
public void dismiss() {
exitAnimation();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
dismiss();
} return super.onKeyDown(keyCode, event);
}
}
package cn.zszh.customs.view;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
public class TextView extends android.widget.TextView{
private Context mContext;
private int mWidth=30;
private int mHeight=30;
//保存设置的图片
private Drawable mLeft, mTop, mRight, mBottom;
public TextView(Context context) {
this(context,null);
}
public TextView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext=context;
init();
}
private void init() {
float density=mContext.getResources().getDisplayMetrics().density;
//将dip转化成px
mWidth*=density;
mHeight*=density;
//手动调用该方法,设置文字上下左右方向的图片宽高
setCompoundDrawablesWithIntrinsicBounds(mLeft, mTop, mRight, mBottom);//设置图片的宽高
}
/**
* 重写该方法,文字上下左右方向的图片大小设置
* @param left
* @param top
* @param right
* @param bottom
*/
@Override
public void setCompoundDrawablesWithIntrinsicBounds( Drawable left, Drawable top, Drawable right, Drawable bottom) {
if (left != null) {
left.setBounds(0, 0, mWidth, mHeight);
mLeft=left;
}
if (right != null) {
right.setBounds(0, 0, mWidth, mHeight);
mRight=right;
}
if (top != null) {
top.setBounds(0, 0, mWidth, mHeight);
mTop=top;
}
if (bottom != null) {
bottom.setBounds(0, 0, mWidth, mHeight);
mBottom=bottom;
}
setCompoundDrawables(left, top, right, bottom);//设置图片的宽高
}
/**
* 设置图片的宽高
* @param width
* @param height
*/
public void setBounds(int width,int height){
mWidth=width;
mHeight=height;
init();
}
}
好了,将上面的代码复制到相应的位置就可以实现如上图的效果,具体分享功能需要自己动手做了(图中的几个分享图片资源需要自己找)。