首页 > 系统 > Android > 正文

PopupWindow使用方法详解

2019-12-12 02:08:26
字体:
来源:转载
供稿:网友

学习了Android PopupWindow的使用技巧 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单,然后自己进行了一下研究,写一个总结,方便以后学习。

效果图:


1.PopupWindow的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:background="@color/colorAccent"  android:gravity="center"  android:layout_width="match_parent"  android:layout_height="wrap_content">  <TextView    android:id="@+id/tv_popup_text"    android:layout_width="wrap_content"    android:layout_height="80dp"    android:text="我就是弹窗"    android:textSize="25sp"    android:textColor="#ffffffff"    android:layout_centerInParent="true"    android:gravity="center"/></LinearLayout>

2.在res下新建anim文件夹,为窗口弹出消失写动画:

popupwindow_in:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">  <translate    android:duration="250"    android:fromYDelta="100.0%"    android:toYDelta="0.0" /></set>

popupwindow_out:

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

添加style:

 <style name="anim_popup_window">    <item name="android:windowEnterAnimation">@anim/popupwindow_in</item>    <item name="android:windowExitAnimation">@anim/popupwindow_out</item>  </style>

3.主界面布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:gravity="center"  android:orientation="vertical"  android:id="@+id/layout_home"  android:background="#FFB5C5"  tools:context="com.lotus.popupwindowdemo.HomeActivity">  <TextView    android:id="@+id/tv_show_popup_window"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="20sp"    android:text="点击显示PopupWindow" /></LinearLayout>

4.主界面代码:

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.Toast;public class HomeActivity extends AppCompatActivity implements View.OnClickListener {  private LinearLayout layout_home;  private TextView tv_show_popup_window;  private PopupWindow mPopupWindow;  private TextView tv_popup_text;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_home);    // 引入窗口配置文件:即弹窗的界面    View popupView = getLayoutInflater().inflate( R.layout.layout_popupwindow, null);    popupView.setOnClickListener( this);    tv_popup_text = (TextView) popupView.findViewById(R.id.tv_popup_text);    tv_popup_text.setOnClickListener(this);    // PopupWindow实例化    mPopupWindow = new PopupWindow( popupView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);    // 设置PopupWindow是否可触摸(设置为不可触摸,那弹出框内的任何控件都不能进行任何点击等等类似操作)    mPopupWindow.setTouchable( true);    // 设置非PopupWindow区域是否可触摸    // 1.若设置PopupWindow获得焦点和非PopupWindow区域可触摸,但实际上非PopupWindow区域的控件并不能响应点击事件等等    // 2.若设置PopupWindow不可获得焦点,则不管非PopupWindow区域被设置能否触摸,实际上非PopupWindow区域的控件都能响应点击事件等等    // 3.若设置PopupWindow不可获得焦点,非PopupWindow区域被设置能触摸,当点击非PopupWindow区域时能隐藏PopupWindow,而点击返回键并不能隐藏窗口,    //  此时通过按钮只能控制窗口的弹出,并不能控制消失,消失只能通过点击其他非PopupWindow区域    mPopupWindow.setOutsideTouchable( false);    // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框(但目前并没有发现此问题)//    mPopupWindow.setBackgroundDrawable( new BitmapDrawable( getResources(), (Bitmap) null));    // 设置PopupWindow显示和隐藏时的动画    mPopupWindow.setAnimationStyle(R.style.anim_popup_window);    // 设置PopupWindow是否可获得焦点    // 1.如果设置为可获得焦点,不管非PopupWindow区域被设置能否触摸,也会在点击屏幕非PopupWindow区域和点击返回键时,使PopupWindow隐藏    // 2.相反,如果设置为不可获得焦点,在点击屏幕非PopupWindow区域或点击返回键时,都不能使PopupWindow隐藏    mPopupWindow.setFocusable(false);    layout_home = (LinearLayout) this.findViewById(R.id.layout_home);    tv_show_popup_window = (TextView) this.findViewById( R.id.tv_show_popup_window);    tv_show_popup_window.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        if ( mPopupWindow.isShowing()) {          // 隐藏窗口,如果设置了点击窗口外消失,则不需要此方式隐藏          mPopupWindow.dismiss();          tv_show_popup_window.setText("点击显示PopupWindow");        } else {          // 弹出窗口显示内容视图,默认以锚定视图的左下角为起点,这里为点击按钮//        mPopupWindow.showAsDropDown( view);//默认在view(tv_show_popup_window)的下方出现          mPopupWindow.showAtLocation( layout_home, Gravity.BOTTOM, 0, 0);          tv_show_popup_window.setText("点击使PopupWindow消失");        }      }    });  }  @Override  public void onClick(View view) {    switch ( view.getId()){      case R.id.tv_popup_text:        Toast.makeText( getApplicationContext(),"我是PopupWindow内的一个控件",Toast.LENGTH_SHORT).show();        break;    }  }}

注:分析属性时,注释写得有点多,因为发现属性彼此间联系紧密,所以要小心使用才行。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表