PopupWindow

来源:互联网 发布:p2p网贷系统源码由来 编辑:程序博客网 时间:2024/06/06 02:04

对于PopupWindow的使用,感觉不是很重要,但还是记下来把,免得以后用到。

其主要实现的是点击一个按钮另外一个布局中的三个按钮,分别是“确定”,”跳转“和”取消“。然后当点击取消的时候,又缩回原来的一个按钮

MainActivity:

package com.example.day_0921_02;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.ImageButton;import android.widget.PopupWindow;public class MainActivity extends Activity {private ImageButton imageButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageButton = (ImageButton) findViewById(R.id.imageButton);View root = getLayoutInflater().inflate(R.layout.popu_layout, null);final PopupWindow pWindow = new PopupWindow(root, 200, 200);imageButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {pWindow.showAsDropDown(v);pWindow.showAtLocation(imageButton, Gravity.CENTER, 20, 20);}});root.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {pWindow.dismiss();}});}}
两个布局文件也发一下吧。

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"    >    <ImageButton         android:id="@+id/imageButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher"        /></RelativeLayout>

第二个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" >    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="确定"/>    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳转"/>    <Button         android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="取消"/></LinearLayout>


0 0
原创粉丝点击