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按钮
 
  
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"
/>
android :layout_width= "300dp"
android :layout_height= "wrap_content"
android :text= " 第二个按钮 "
android :background= "#ffaaff"
android :layout_gravity= "center_horizontal"
android :layout_marginTop= "10dp"
/>
android :layout_width= "300dp"
android :layout_height= "wrap_content"
android :text= " 第三个按钮 "
android :background= "#ffaaff"
android :layout_gravity= "center_horizontal"
android :layout_marginTop= "10dp"
/>
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
 
  
android :duration= "1000"
android :fromYDelta= "0"
android :toYDelta= "50%p"
/>
android :duration= "1000"
android :fromAlpha= "1.0"
android :toAlpha= "0.0"
/>
第二个文件popshow_anim
 
  
 
  
 
  
android :duration= "1000"
android :fromYDelta= "100%p"
android :toYDelta= "0"
/>
android :duration= "1000"
android :fromAlpha= "0.0"
android :toAlpha= "1.0"
/>
之后在Styles下写入显示动画和消失动画
 
  
 
  
< style name= "mypopwindow_anim_style" >
	name="android:windowEnterAnimation">@anim/popshow_anim    
	    
	name="android:windowExitAnimation">@anim/pophidden_animstyle>

下面是最后一步 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() ;
}
}) ;
}
}
第一个界面 第二个界面
PopupWindow基本展示,Button底部展示_第1张图片 PopupWindow基本展示,Button底部展示_第2张图片

你可能感兴趣的:(PopupWindow基本展示,Button底部展示)