手把手教会popupWindow从下往上弹出效果的实现

来源:互联网 发布:类似光环助手的软件 编辑:程序博客网 时间:2024/04/29 22:21

效果如图所示,点击开始按钮,popWindow从下往上出来,再点击popWindow外面,popWindow又从上往下消失

可以看出来,上面的popupWindow是半透明的,后面我会细说。

最基本的是activity_main了,很简单,就只是一个button,这里我就不贴代码了。

接下来的是,popWindow的界面了

代码如下: 这里注意我里面的那个注释

<?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="wrap_content"  android:layout_margin="5dp"  android:orientation="vertical" >  <!-- 这里的linearLayout加android:background=""这个属性要谨慎,如果加了后,popwindow是不能半透明了的 -->  <Button    android:id="@+id/first"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginBottom="5dp"    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"    android:layout_marginTop="5dp"    android:background="@android:color/holo_red_light"    android:text="第一个按钮" />  <Button    android:id="@+id/second"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginBottom="5dp"    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"    android:background="@android:color/holo_red_light"    android:text="第二个按钮" />  <Button    android:id="@+id/third"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginBottom="5dp"    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"    android:background="@android:color/holo_red_light"    android:text="第三个按钮" /></LinearLayout>

然后在res/下新建一个文件夹anim,进而anim下新建两个xml文件,如图所示:


其中,pophidden_anim的代码如下

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">     <translate    android:duration="1000"    android:fromYDelta="0"    android:toYDelta="50%p" />  <alpha    android:duration="1000"    android:fromAlpha="1.0"    android:toAlpha="0.0" /></set>

popshow_anim的代码如下

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">   <translate    android:duration="1000"    android:fromYDelta="100%p"    android:toYDelta="0" />  <alpha    android:duration="1000"    android:fromAlpha="0.0"    android:toAlpha="1.0" /></set>

然后在values/styles.xml加入以下代码,变成这个样子,上面的那些是自带的

<resources>  <!--    Base application theme, dependent on API level. This theme is replaced    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.  -->  <style name="AppBaseTheme" parent="android:Theme.Light">    <!--      Theme customizations available in newer API levels can go in      res/values-vXX/styles.xml, while customizations related to      backward-compatibility can go here.    -->  </style>  <!-- Application theme. -->  <style name="AppTheme" parent="AppBaseTheme">    <!-- All customizations that are NOT specific to a particular API-level can go here. -->  </style>   <!--  这个是加入的代码 -->  <style name="mypopwindow_anim_style">    <item name="android:windowEnterAnimation">@anim/popshow_anim</item> <!-- 指定显示的动画xml -->    <item name="android:windowExitAnimation">@anim/pophidden_anim</item> <!-- 指定消失的动画xml -->  </style></resources>

之后就是Activity里面的代码了,我里面都写好了所有的注释,应该可以看得很清楚

package com.example.popwindow;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.ColorDrawable;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.WindowManager;import android.widget.Button;import android.widget.PopupWindow;import android.widget.PopupWindow.OnDismissListener;import android.widget.Toast;public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    Button start = (Button) findViewById(R.id.start);    start.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        showPopwindow();      }    });  }  /**   * 显示popupWindow   */  private void showPopwindow() {    // 利用layoutInflater获得View    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);    View view = inflater.inflate(R.layout.popwindowlayout, null);    // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()    PopupWindow window = new PopupWindow(view,        WindowManager.LayoutParams.MATCH_PARENT,        WindowManager.LayoutParams.WRAP_CONTENT);    // 设置popWindow弹出窗体可点击,这句话必须添加,并且是true    window.setFocusable(true);    // 实例化一个ColorDrawable颜色为半透明    ColorDrawable dw = new ColorDrawable(0xb0000000);    window.setBackgroundDrawable(dw);        // 设置popWindow的显示和消失动画    window.setAnimationStyle(R.style.mypopwindow_anim_style);    // 在底部显示    window.showAtLocation(MainActivity.this.findViewById(R.id.start),        Gravity.BOTTOM, 0, 0);    // 这里检验popWindow里的button是否可以点击    Button first = (Button) view.findViewById(R.id.first);    first.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        System.out.println("第一个按钮被点击了");      }    });        //popWindow消失监听方法    window.setOnDismissListener(new OnDismissListener() {            @Override      public void onDismiss() {        System.out.println("popWindow消失");      }    });  }}

其中window.setFocusable(true)和window.setBackgroundDrawable()必须填写,如果是想让popWindow半透明,就是上面的那个方法,

如果只是单纯的调用这个方法就这样写window.setBackgroundDrawable(new BitmapDrawable());

1 0