安卓popwindow仿微信代码
时间: 2023-11-20 10:10:26 浏览: 91
以下是一个简单的 Android PopupWindow 实现,类似于微信的弹出菜单:
1. 首先,在你的布局文件中添加一个按钮或者其他可点击的视图:
```xml
<Button
android:id="@+id/btn_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹出菜单" />
```
2. 在你的 Activity 或者 Fragment 中,初始化 PopupWindow 对象和布局:
```java
private PopupWindow popupWindow;
private View popupView;
private void initPopupWindow() {
// 初始化 PopupWindow 布局
popupView = getLayoutInflater().inflate(R.layout.popup_window, null);
// 初始化 PopupWindow 对象
popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
```
3. 给按钮添加点击事件,弹出 PopupWindow:
```java
Button btnPopup = findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 显示 PopupWindow
popupWindow.showAsDropDown(v);
}
});
```
4. 在 PopupWindow 布局中添加菜单项:
```xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="菜单项1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="菜单项2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="菜单项3" />
</LinearLayout>
```
这样就实现了一个简单的 Android PopupWindow,类似于微信的弹出菜单。你可以根据自己的需求,修改布局和样式。
阅读全文
相关推荐

















