您的位置:首页 > 产品设计 > UI/UE

androidUI——PopWindow

2015-11-09 18:44 344 查看
3个文件,MainActivity.java,activity_main.xml,popup_window.xml

MainActivity:

package com.example.popwindowdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
/**
*
* @author H100
* PopWindow和AlertDialogDialog的区别
* Alertdialog居中显示,PopWindow不居中显示,显示在指定位置
* AlertDialog会阻塞线程,比如Ui,PopWindow不会
*
*/
public class MainActivity extends Activity implements OnClickListener{
Button btn_one,btn_three,btn_at;
PopupWindow pop;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initButton();
initPopwindow();
}

void initButton(){
btn_one=(Button) findViewById(R.id.btnShowAsDrawDown);
btn_three=(Button) findViewById(R.id.btnShowAsDrawDown1);
btn_at=(Button) findViewById(R.id.btnShowAt);
btn_at.setOnClickListener(this);
btn_one.setOnClickListener(this);
btn_three.setOnClickListener(this);
}
void initPopwindow(){
View view=LayoutInflater.from(this).inflate(R.layout.popup_window, null);
/**
* 关于layoutParams的设置方法一般就是设置父控件的LayoutParams,下面的竟然也能用,也就是父父的也可以
*/
pop=new PopupWindow(view, LinearLayout.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setOutsideTouchable(true);
Button btn=(Button) view.findViewById(R.id.btn_pop);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
pop.dismiss();
Toast.makeText(MainActivity.this, "popwindow手动退出", 500).show();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnShowAsDrawDown:
if(pop.isShowing()){
pop.dismiss();
}else{
pop.showAsDropDown(v);
}
break;
case R.id.btnShowAsDrawDown1:
if(pop.isShowing()){
pop.dismiss();
}else{
pop.showAsDropDown(v,0,-320);//起始点向上平移320
}
break;
case R.id.btnShowAt:
if(pop.isShowing()){
pop.dismiss();
}else{
//为了让这个水平平移效果不被FillParent遮盖就干脆设置成Wrap
pop.setWindowLayoutMode(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER_HORIZONTAL, 100, 50);
}
break;

default:
break;
}

}

}


activity_main.xml:

<LinearLayout 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"
android:id="@+id/main"
android:orientation="vertical"
tools:context="com.example.popwindowdemo.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/btnShowAsDrawDown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show as drawdown(one parameter)" />

<Button
android:id="@+id/btnShowAsDrawDown1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show as drawdown(three parameter)" />

<Button
android:id="@+id/btnShowAt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show at location" />

</LinearLayout>


popup_window.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"
android:background="#ff9aaab7">
<!-- 突出显示所以设置颜色要不就透明了 -->
<Button android:id="@+id/btn_pop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="dismiss"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="none"/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: