在我们做分享功能的时候,经常会需要自定义一些视图去实现分享,下面是根据公司的要求,写的一个popupwindow,如有不妥的地方,欢迎大家给出意见。
package com.chen.demo;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.PopupWindow;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_id).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view=getLayoutInflater().from(this).inflate(R.layout.activity_main2,null);
int a=getDaoHangHeight(this);
final PopupWindow pw=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pw.setBackgroundDrawable(new BitmapDrawable());
Myimage myimage=new Myimage(MainActivity.this);
pw.showAtLocation(view, Gravity.BOTTOM,0,a);
view.findViewById(R.id.img1_id).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Myimage.showToast(MainActivity.this, "您好,消息已发到朋友圈!");
pw.dismiss();
}
});
view.findViewById(R.id.img2_id).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Myimage.showToast(MainActivity.this, "您好,消息已发到微博朋友圈!");
pw.dismiss();
}
});
view.findViewById(R.id.img3_id).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Myimage.showToast(MainActivity.this, "您好,消息已发到微信朋友圈!");
pw.dismiss();
}
});
}
//获取导航栏的高度
public static int getDaoHangHeight(Context context) {
int resourceId=0;
int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
if (rid!=0){
resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}else
return 0;
}
}
来源:https://blog.csdn.net/believe_chen/article/details/102777064