手把手教會(huì)popupWindow從下往上彈出效果的實(shí)現(xiàn)

效果如圖所示,點(diǎn)擊開始按鈕,popWindow從下往上出來,再點(diǎn)擊popWindow外面,popWindow又從上往下消失



可以看出來,上面的popupWindow是半透明的,后面我會(huì)細(xì)說。
最基本的是activity_main了,很簡(jiǎn)單,就只是一個(gè)button,這里我就不貼代碼了。
接下來的是,popWindow的界面了



代碼如下: 這里注意我里面的那個(gè)注釋
<?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=""這個(gè)屬性要謹(jǐn)慎,如果加了后,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="第一個(gè)按鈕" />

  <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="第二個(gè)按鈕" />

  <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="第三個(gè)按鈕" />

</LinearLayout>

然后在res/下新建一個(gè)文件夾anim,進(jìn)而anim下新建兩個(gè)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加入以下代碼,變成這個(gè)樣子,上面的那些是自帶的

<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>

   <!--  這個(gè)是加入的代碼 -->
  <style name="mypopwindow_anim_style">
    <item name="android:windowEnterAnimation">@anim/popshow_anim</item>
 <!-- 指定顯示的動(dòng)畫xml -->

    <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
 <!-- 指定消失的動(dòng)畫xml -->
  </style>

</resources>

之后就是Activity里面的代碼了,我里面都寫好了所有的注釋,應(yīng)該可以看得很清楚

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);

    // 設(shè)置popWindow彈出窗體可點(diǎn)擊,這句話必須添加,并且是true
    window.setFocusable(true);


    // 實(shí)例化一個(gè)ColorDrawable顏色為半透明
    ColorDrawable dw = new ColorDrawable(0xb0000000);
    window.setBackgroundDrawable(dw);

    
    // 設(shè)置popWindow的顯示和消失動(dòng)畫
    window.setAnimationStyle(R.style.mypopwindow_anim_style);
    // 在底部顯示
    window.showAtLocation(MainActivity.this.findViewById(R.id.start),
        Gravity.BOTTOM, 0, 0);

    // 這里檢驗(yàn)popWindow里的button是否可以點(diǎn)擊
    Button first = (Button) view.findViewById(R.id.first);
    first.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        System.out.println("第一個(gè)按鈕被點(diǎn)擊了");
      }
    });
    
    //popWindow消失監(jiān)聽方法
    window.setOnDismissListener(new OnDismissListener() {
      
      @Override
      public void onDismiss() {
        System.out.println("popWindow消失");
      }
    });

  }
}

其中window.setFocusable(true)和window.setBackgroundDrawable()必須填寫,如果是想讓popWindow半透明,就是上面的那個(gè)方法,
如果只是單純的調(diào)用這個(gè)方法就這樣寫window.setBackgroundDrawable(new BitmapDrawable());
關(guān)于popupWindow顯示位置的具體方法,你可以看這個(gè)博客 popupWindow在指定位置上的顯示

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容