一直很想模仿QQ的底部弹出效果,虽然咱也实现了,就是山寨了点,请各位谅解技术水平。本文是用popupwindow控件实现效果的,先上图,无图无真相。
接着上代码,就是一个button按钮点击而已
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
很简单
然后我们看popupwindow的布局,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50000000" >
<LinearLayout
android:id="@+id/address_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#efeeee"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/stytem_choose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@drawable/round_shape_white_coner"
android:gravity="center"
android:padding="15dp"
android:text="版本升级"
android:textColor="#ff444444"
android:textSize="16dp" />
<TextView
android:id="@+id/self_choose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/round_shape_white_coner"
android:gravity="center"
android:padding="15dp"
android:text="退出QQ"
android:textColor="#ff444444"
android:textSize="16dp" />
<TextView
android:id="@+id/cancle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@drawable/round_shape_gary_corner"
android:gravity="center"
android:padding="15dp"
android:text="取消"
android:textColor="#000000"
android:textSize="16dp" />
</LinearLayout>
</RelativeLayout>
代码简单,不解释。
然后我们再看activity怎么用的。
public class MainActivity extends Activity {
private Button button;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
showPopu();
}
});
}
private void showPopu(){
View v = LayoutInflater.from(this).inflate(
R.layout.view_popupwindow, null);
popupWindow = new PopupWindow(v, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
// 设置点击窗口外边窗口消失
popupWindow.setOutsideTouchable(true);
// 设置此参数获得焦点,否则无法点击
popupWindow.setFocusable(true);
if (popupWindow.isShowing()) {
// 隐藏窗口,如果设置了点击窗口外小时即不需要此方式隐藏
popupWindow.dismiss();
} else {
// 显示窗口
popupWindow.showAtLocation(button, Gravity.FILL, 0, 0);
}
}
}
到此大功告成!