Android 底部弹出提示框的解决办法(使用Activity以及PopupWindow)

时间:2014-06-22 18:12:52   收藏:0   阅读:308

本片文章主要谈探讨了如何实现在底部弹出提示框背景为半透明效果的实现。想要实现此种效果一般有两种方式一个是使用Activity设置Theme另一种方式就是使用PopupWindow设置样式实现效果。

一,使用Activity

首先是此activity的布局文件

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:gravity="center_horizontal"  
    android:orientation="vertical"  
    android:background="#b0000000"
  >  
  
<LinearLayout    
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:id="@+id/pop_layout"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:gravity="center_horizontal"  
    android:orientation="vertical"  
    android:layout_alignParentBottom="true" 
    android:background="@drawable/callsharp_select"  
     >  
  
       
    <Button  
        android:padding="10dp"
        android:id="@+id/btn_take_photo"  
        android:layout_marginLeft="30dip"  
        android:layout_marginRight="30dip"  
        android:layout_marginTop="20dip"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="拍照"  
        android:background="@drawable/whiteselector"  
        android:textStyle="bold"  
         />  
  
    <Button  
        android:padding="10dp"
        android:id="@+id/btn_pick_photo"  
        android:layout_marginLeft="30dip"  
        android:layout_marginRight="30dip"  
        android:layout_marginTop="5dip"    
         android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="从相册选择"  
         android:background="@drawable/whiteselector"  
         android:textStyle="bold"  
         />  
  
    <Button  
        android:padding="10dp"
        android:id="@+id/btn_cancel"  
       android:layout_marginLeft="30dip"  
       android:layout_marginRight="30dip"  
       android:layout_marginTop="15dip"    
       android:layout_marginBottom="15dip"  
       android:layout_width="fill_parent"  
       android:layout_height="wrap_content"  
       android:text="取消"  
       android:background="@drawable/whiteselector"  
       android:textStyle="bold"  
          
        />  
</LinearLayout>  
</RelativeLayout>  
在AndroidManifest当中的配置
<activity 
            android:theme="@style/activity_popup"
            android:name=".PopupActivity"
            ></activity>
指定了自定义的Theme 在style当中设置
<style name="activity_popup" parent="android:Theme.Translucent.NoTitleBar">
    <item name="android:windowAnimationStyle">@style/AnimBottom</item>
    </style>
在其中我添加了进入很退出的动画采用了上下进出的方式:

<style name="AnimBottom" parent="@android:style/Animation">  
    <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>  
    <item name="android:windowExitAnimation">@anim/push_bottom_out</item>  
    </style>

进出的动画为:

push_bottom_in:

<?xml version="1.0" encoding="utf-8"?>  
<!-- 上下滑入式 -->  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
  
    <translate  
        android:duration="200"  
        android:fromYDelta="100%p"  
        android:toYDelta="0"           
     />         
</set>  
push_bottom_out为:
<?xml version="1.0" encoding="utf-8"?>  
<!-- 上下滑出式 -->  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
  
       
    <translate  
        android:duration="200"  
        android:fromYDelta="0"  
        android:toYDelta="50%p" />  
</set>
这样就实现了下部弹出框。


二,使用PopupWindow实现

使用的样式和动画效果不改变与上边的相同,首先一个自定义的PopupWindow

package com.example.activityanimationdemo;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;

public class MyPopupWindow extends PopupWindow{
	private View mainview;
public MyPopupWindow(Activity context,OnClickListener itemclick){
	super(context);
	LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
	mainview=inflater.inflate(R.layout.activity_popup, null);
	this.setWidth(LayoutParams.MATCH_PARENT);
	this.setHeight(LayoutParams.MATCH_PARENT);
	this.setContentView(mainview);
	this.setFocusable(true);
	this.setAnimationStyle(R.style.AnimBottom);
}
}
然后在Activity当中的点击事件显示PopupWindow:
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
//				ActivityMy.this.startActivity(new Intent(ActivityMy.this, PopupActivity.class));
		popupwindow=new MyPopupWindow(ActivityMy.this, null);
		popupwindow.showAtLocation(ActivityMy.this.findViewById(R.id.demo), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
			}
		});
	}
效果图:

bubuko.com,布布扣

Android 底部弹出提示框的解决办法(使用Activity以及PopupWindow),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!