popupwindow实现弹出层

来源:互联网 发布:百度网盘mac版哪里设置 编辑:程序博客网 时间:2024/05/05 16:46

实现的效果描述:

当点击弹出层按钮后,弹出一个层popup1覆盖在主activity上,与此同时,在popup1和主activity之间再弹出一个popup2透明层,使得主activity失去焦点,只能点击popup1上的

控件。

popupwindow提供了显示提供了两种形式:

1-->showAtLocation()显示在指定位置,有两个方法重载:
public void showAtLocation(View parent, int gravity, int x, int y)
public void showAtLocation(IBinder token, int gravity, int x, int y)
2-->showAsDropDown()显示在一个参照物View的周围,有三个方法重载:
public void showAsDropDown(View anchor)
public void showAsDropDown(View anchor, int xoff, int yoff)
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
最后一种带Gravity参数的方法是API 19新引入的。

这是主Activity的源码:

public classMainActivity extends Activity{


private ImageView pop=null; 
// 声明PopupWindow对象的引用 
private PopupWindow popupWindow; 
//覆盖在主界面上的透明层
private PopupWindow transPopupWindow; 
View popupWindow_view1 = null;


/* (non-Javadoc)
* @seeandroid.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(BundlesavedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity_layout);



init();
}

private void init(){
// 点击按钮弹出菜单 
pop = (ImageView) findViewById(R.id.popBtn); 
pop.setOnClickListener(listener);
}

private OnClickListener listener = newOnClickListener() {

@Override
public void onClick(View v) {
if(v.getId()==R.id.popBtn){
//获取某一个控件在当前父窗口的坐标
final int[] location = new int[2]; 
pop.getLocationOnScreen(location);
//显示一个透明层,夹在弹出层和主界面之间,使主界面失去焦点
getTransPopupWindow();
transPopupWindow.showAtLocation(findViewById(R.id.parentID),Gravity.NO_GRAVITY,location[0], location[1]);

//显示弹出层
getPopupWindow(); 
popupWindow.showAtLocation(findViewById(R.id.parentID),Gravity.NO_GRAVITY,location[0], location[1]);
}
else if(v.getId()==R.id.open){
// 这里可以执行相关操作 
System.out.println("打开操作");
// 对话框消失 
popupWindow.dismiss();
transPopupWindow.dismiss();
}
else if(v.getId()==R.id.save){
// 这里可以执行相关操作 
System.out.println("保存操作"); 
popupWindow.dismiss(); 
transPopupWindow.dismiss();
}
}
};

protected void inittransPopupWindow() {
popupWindow_view1 =getLayoutInflater().inflate(R.layout.trans, null, false);
transPopupWindow = newPopupWindow(popupWindow_view1,findViewById(R.id.parentID).getWidth(),
findViewById(R.id.parentID).getHeight(), true);
transPopupWindow.setAnimationStyle(R.style.AnimationFade);
}

protected void initPopuptWindow() { 
// TODO Auto-generated method stub 
// 获取自定义布局文件pop.xml的视图 
View popupWindow_view = getLayoutInflater().inflate(R.layout.pop,null, false); 

//创建PopupWindow实例,设置popupWindow的宽和高分别为父控件的对应宽和高
popupWindow = new PopupWindow(popupWindow_view,findViewById(R.id.parentID).getWidth()/2,findViewById(R.id.parentID).getHeight(), true);
popupWindow.setAnimationStyle(R.style.AnimationFade); 

// pop.xml视图里面的控件 
Button open = (Button)popupWindow_view.findViewById(R.id.open); 
Button save = (Button)popupWindow_view.findViewById(R.id.save);

open.setOnClickListener(listener);
save.setOnClickListener(listener);
}

/*** 
* 获取PopupWindow实例 
*/ 
private void getPopupWindow() { 
if (null != popupWindow) { 
popupWindow.dismiss(); 
return; 
}
else { 
initPopuptWindow(); 



private void getTransPopupWindow() { 
if (null != transPopupWindow) { 
transPopupWindow.dismiss(); 
return; 
}
else { 
inittransPopupWindow(); 




}

布局文件:

<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/parentID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
     <RelativeLayout
      
        android:layout_width="match_parent"
       android:layout_height="@dimen/title_height"
        android:background="@drawable/action_bar_bg_tile_horizontal"
        android:orientation="horizontal" >


        <ImageView
            android:id="@+id/popBtn"
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:layout_alignParentLeft="true"
           android:layout_alignParentTop="true"
           android:background="@drawable/personalsetting" />

        <TextView
           android:layout_width="wrap_content"
            android:layout_height="match_parent"
           android:layout_marginLeft="66dp"
           android:layout_toRightOf="@+id/popBtn"
           android:gravity="center_vertical"
            android:text="111"
           android:textColor="@color/white"
            android:textSize="@dimen/actionbar_textsize"
            android:textStyle="bold"/>
    </RelativeLayout>

</LinearLayout>

弹出层布局文件:

<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C9C4C4"
android:orientation="vertical" >

<Button android:id="@+id/open" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/open"/> 

<Button android:id="@+id/save" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/save"/> 

</LinearLayout>

弹出透明层trans.xml

<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/trans"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="#00000000">
</LinearLayout>
styles.xml

  <stylename="AnimationFade"> 
<!-- PopupWindow左右弹出的效果--> 
<itemname="android:windowEnterAnimation">@anim/in_lefttoright</item> 
<itemname="android:windowExitAnimation">@anim/out_righttoleft</item> 
</style> 

in_lefttoright.xml

<?xmlversion="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- 定义从左向右进入的动画 --> 
<translate 
android:fromXDelta="-100%" 
android:toXDelta="0" 
android:duration="500"/> 
</set> 

out_righttoleft.xml

<?xmlversion="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- 定义从右向左动画退出动画 --> 
<translate 
android:fromXDelta="0" 
android:toXDelta="-100%" 
android:duration="500"/> 
</set> 

0 0
原创粉丝点击