package com.bruce.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
private PopupWindow mPopWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
Button mBtn = (Button) findViewById(R.id.test_pop);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mPopWindow == null) {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_popwindow_friend, null);
mPopWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mPopWindow.setBackgroundDrawable(new BitmapDrawable());
contentView.findViewById(R.id.add_friend).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
mPopWindow.dismiss();
}
});
contentView.findViewById(R.id.friend_active).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
mPopWindow.dismiss();
}
});
}
mPopWindow.setOutsideTouchable(true);
// mPopWindow.showAsDropDown(view, 100, 10);
if (mPopWindow.isShowing()) {
mPopWindow.dismiss();
} else {
mPopWindow.showAsDropDown(view, 20, 20);
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/test_pop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
item_popubwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/add_friend"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加好友" />
</LinearLayout>
<LinearLayout
android:id="@+id/friend_active"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="好友动态" />
</LinearLayout>
</LinearLayout>