PupWindows案例

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>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值