PopupWindow(泡泡)

来源:互联网 发布:学习编程语言的网站 编辑:程序博客网 时间:2024/04/19 17:25
public class MainActivity extends Activity implements OnClickListener {    private PopupWindow popup;    private View view;    private View view1;    private Button deng1;    private Button zhu2;    private View view2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        deng1 = (Button) findViewById(R.id.deng_01);        deng1.setOnClickListener(this);        view = View.inflate(MainActivity.this,R.layout.pop, null);        popup = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        popup.setOutsideTouchable(true);        popup.setBackgroundDrawable(new BitmapDrawable());        popup.setFocusable(true);        //        zhu2 = (Button) findViewById(R.id.zhu_01);        zhu2.setOnClickListener(this);        view1 = View.inflate(MainActivity.this,R.layout.zhuche, null);        view2 = View.inflate(MainActivity.this,R.layout.activity_main, null);        popup = new PopupWindow(view1, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        popup.setOutsideTouchable(true);        popup.setBackgroundDrawable(new BitmapDrawable());        popup.setFocusable(true);        Button button = (Button) view1.findViewById(R.id.button);        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                Toast.makeText(MainActivity.this," 注册 ",Toast.LENGTH_LONG).show();            }        });    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.deng_01:            popup.showAsDropDown(deng1);            break;        case R.id.zhu_01:            popup.showAtLocation(view2, Gravity.TOP, 0, 100);            break;        default:            break;        }    }}

activity

<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"  >    <LinearLayout         android:layout_centerInParent="true"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:orientation="horizontal"        >    <Button         android:id="@+id/deng_01"        android:layout_alignParentRight="true"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="登陆"          />    <Button         android:id="@+id/zhu_01"        android:layout_alignParentRight="true"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="快速注册"          />    </LinearLayout></RelativeLayout>

pop

<?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"     android:background="#FFA980"    >    <LinearLayout         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical"        >    <EditText         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入用户名"        />    <EditText         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入密码"        />    <Button         android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="登陆"        />    </LinearLayout></RelativeLayout>

zhuche

<?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"    android:background="#00CCCC"     >     <LinearLayout         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical"        >    <EditText         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入用户名"        />    <EditText         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入密码"        />    <Button         android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/button"        android:text="注册"        /></LinearLayout></RelativeLayout>
原创粉丝点击