PopupWindow基本展示,Button底部展示

主要转载:http://blog.csdn.net/harryweasley/article/details/41283129
我是在这兄弟的基础上加了点东西,第一次写博客 写的不好请多谅解。有不好的地方地方请多多指教
写的很low请多原谅

主布局 就是一个Button
 
 
< Button
android :id= "@+id/btn_popwindow"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= " 点击 "
/>
然后创建PopupWindow的4个Button按钮
  
  
<Button
android :id= "@+id/first"
android :layout_width= "300dp"
android :layout_height= "wrap_content"
android :text= " 第一个按钮 "
android :background= "#ffaaff"
android :layout_gravity= "center_horizontal"
android :layout_marginTop= "10dp"
/>
<Button
android :layout_width= "300dp"
android :layout_height= "wrap_content"
android :text= " 第二个按钮 "
android :background= "#ffaaff"
android :layout_gravity= "center_horizontal"
android :layout_marginTop= "10dp"
/>
<Button
android :layout_width= "300dp"
android :layout_height= "wrap_content"
android :text= " 第三个按钮 "
android :background= "#ffaaff"
android :layout_gravity= "center_horizontal"
android :layout_marginTop= "10dp"
/>
<Button
android :layout_width= "300dp"
android :layout_height= "wrap_content"
android :text= " 第四个按钮 "
android :background= "#ffaaff"
android :layout_gravity= "center_horizontal"
android :layout_marginTop= "10dp"
/>

在res文件下在创建anim需要创建PopWindow淡入淡出效果
第一个文件pophidden_anim
  
  
<translate
android :duration= "1000"
android :fromYDelta= "0"
android :toYDelta= "50%p"
/>
<alpha
android :duration= "1000"
android :fromAlpha= "1.0"
android :toAlpha= "0.0"
/>
第二个文件popshow_anim
   
   
<translate
android :duration= "1000"
android :fromYDelta= "100%p"
android :toYDelta= "0"
/>
<alpha
android :duration= "1000"
android :fromAlpha= "0.0"
android :toAlpha= "1.0"
/>
之后在Styles下写入显示动画和消失动画
    
    
< style name= "mypopwindow_anim_style" >
<!--指示显示动画
ExitAnimation 不要写反 写反不会出现平滑过度效果 ( 如果写反会造成先展示个隐藏在展示 button,
如果取消会造成在消失后再次展示 )
     
     
popshow_anim
	pophidden_anim 这两个是我在res下面定义的两个xml上面有说-->    
	<item name="android:windowEnterAnimation">@anim/popshow_anim</item>    
	<!--指示消失动画    ExitAnimation    -->    
	<item name="android:windowExitAnimation">@anim/pophidden_anim</item></style>

下面是最后一步 Activity
    
    
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main ) ;
Button btn = (Button) findViewById(R.id. btn_popwindow ) ;
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick (View view) {
showPopwindow() ;
}
}) ;
}
private void showPopwindow () {
//利用LayoutInflater 获取view
LayoutInflater inflater = (LayoutInflater) getSystemService(Context. LAYOUT_INFLATER_SERVICE ) ;
View view = inflater.inflate(R.layout. popupwindow_activity ,null ) ;
//两种方法得到宽度和高度
PopupWindow window = new PopupWindow(view ,
WindowManager.LayoutParams. MATCH_PARENT ,
WindowManager.LayoutParams. WRAP_CONTENT
) ;
//设置popwindow弹窗可点击 这句话必须添加,并且为true
window.setFocusable( true ) ;
//实例化一个ColorDrawer颜色为半透明
ColorDrawable color = new ColorDrawable( 0x0000000 ) ;
window.setBackgroundDrawable(color) ;
//设置popwindow消失显示动画
window.setAnimationStyle(R.style. mypopwindow_anim_style ) ;
//在底部显示
window.showAtLocation(MainActivity. this .findViewById(R.id. btn_popwindow ) ,
Gravity. BOTTOM , 0 , 0 ) ;
//设置检验popwindow里的button是否可以点击
Button first = (Button) view.findViewById(R.id. first ) ;
first.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick (View view) {
Toast. makeText (MainActivity. this, " 第一个按钮被点击了 " , Toast. LENGTH_SHORT ).show() ;
}
}) ;
window.setOnDismissListener( new PopupWindow.OnDismissListener() {
@Overr ide
public void onDismiss () {
Toast. makeText (MainActivity. this, "PopWindow 消失了 " , Toast. LENGTH_SHORT ).show() ;
}
}) ;
}
}
第一个界面 第二个界面
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值