android实现弹出窗口的方式有两种,第一种用Activity实现弹出窗口的效果,第二种用PopupWindow组件实现
1,用activity方式实现
public class DialogActivity extends Activity implements OnClickListener {
private LinearLayout commentView;
private LinearLayout shareView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_youji);
commentView = (LinearLayout) findViewById(R.id.comment_div);
shareView = (LinearLayout) findViewById(R.id.share_div);
commentView.setOnClickListener(this);
shareView.setOnClickListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return true;
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.comment_div:
Toast.makeText(getApplicationContext(), "comment",
Toast.LENGTH_SHORT).show();
break;
case R.id.share_div:
Toast.makeText(getApplicationContext(), "collect",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/comment_div"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp" &g