2个Java类1个activity_main.xml主页界面,一个pupwindows.xml布局
1.是MainActivity显示界面
2.把Pupwindows分离出
MyPupWindows.java
public class MyPupWindows extends PopupWindow {
private int screen_wide;//屏幕宽度
public MyPupWindows(View view,Activity cActivity) {
Display display = cActivity.getWindowManager().getDefaultDisplay();
screen_wide = display.getWidth();
this.setContentView(view); // 创建PupWindows视图
this.setWidth(screen_wide);//屏幕宽度
this.setHeight(LayoutParams.WRAP_CONTENT);//高度自适应
this.setFocusable(true);//PupWindows视图内获取焦点
this.setOutsideTouchable(true);//点击外部消失
this.setTouchable(true);//获取焦点.让里面的控件可以做监听事件
this.update();// 刷新状态
// 实例化一个ColorDrawable颜色为半透明
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
this.setBackgroundDrawable(new ColorDrawable(0));
}
public void showPopupWindow(View parent) {
if (!this.isShowing()) {
this.showAsDropDown(parent, screen_wide, 0);
} else {
this.dismiss();
}
}
}
<pre name="code" class="java">MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
private Context context;// 上下文
private MyPupWindows popupWindow;
private Button main_btn;
private Button all_ptoto_btn;
private Button first_photo_btn;
private View popup_dialog;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置吧显示标题栏--需要在创建视图之前设置
setContentView(R.layout.activity_main);
context = this;// 上下文赋值
InitView();//1.初始化控件
popupWindow = new MyPupWindows(popup_dialog, MainActivity.this);//1.1创建PupWindows对象
}
private void InitView() {
main_btn = (Button) this.findViewById(R.id.photo_btn);// 获取mainActivity布局的按钮
popup_dialog = View.inflate(context, R.layout.popup_dialog, null);// PupWindows布局
all_ptoto_btn = (Button) popup_dialog.findViewById(R.id.all_ptoto_btn);
first_photo_btn = (Button) popup_dialog.findViewById(R.id.first_photo_btn);
all_ptoto_btn.setOnClickListener(this);
main_btn.setOnClickListener(this);
first_photo_btn.setOnClickListener(this);
}
public void onClick(View v) {//1.2设置监听
switch (v.getId()) {
case R.id.photo_btn:
popupWindow.showPopupWindow(main_btn);
break;
case R.id.all_ptoto_btn:
main_btn.setText("所有相册");
popupWindow.dismiss();
break;
case R.id.first_photo_btn:
main_btn.setText("相册1");
popupWindow.dismiss();
break;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/photo_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="所有相册" />
</RelativeLayout>
popup_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/all_ptoto_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="所有相册" />
<Button
android:id="@+id/first_photo_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="相册1" />
</LinearLayout>
</RelativeLayout>