学习了Android PopupWindow的使用技巧 和【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部d出菜单,然后自己进行了一下研究,写一个总结,方便以后学习。
效果图:
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="我就是d窗" androID:textSize="25sp" androID:textcolor="#ffffffff" androID:layout_centerInParent="true" androID:gravity="center"/></linearLayout>
2.在res下新建anim文件夹,为窗口d出消失写动画:
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); // 引入窗口配置文件:即d窗的界面 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是否可触摸(设置为不可触摸,那d出框内的任何控件都不能进行任何点击等等类似 *** 作) mPopupWindow.settouchable( true); // 设置非PopupWindow区域是否可触摸 // 1.若设置PopupWindow获得焦点和非PopupWindow区域可触摸,但实际上非PopupWindow区域的控件并不能响应点击事件等等 // 2.若设置PopupWindow不可获得焦点,则不管非PopupWindow区域被设置能否触摸,实际上非PopupWindow区域的控件都能响应点击事件等等 // 3.若设置PopupWindow不可获得焦点,非PopupWindow区域被设置能触摸,当点击非PopupWindow区域时能隐藏PopupWindow,而点击返回键并不能隐藏窗口, // 此时通过按钮只能控制窗口的d出,并不能控制消失,消失只能通过点击其他非PopupWindow区域 mPopupWindow.setoutsIDetouchable( false); // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismissd框(但目前并没有发现此问题)// 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 { // d出窗口显示内容视图,默认以锚定视图的左下角为起点,这里为点击按钮// mPopupWindow.showAsDropDown( vIEw);//默认在vIEw(tv_show_popup_window)的下方出现 mPopupWindow.showAtLocation( layout_home,Gravity.BottOM,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; } }}
注:分析属性时,注释写得有点多,因为发现属性彼此间联系紧密,所以要小心使用才行。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的PopupWindow使用方法详解全部内容,希望文章能够帮你解决PopupWindow使用方法详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)