这篇文章主要为大家详细介绍了Android中PopupWindow弹出式窗口的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android中PopupWindow弹出式窗口使用的具体代码,供大家参考,具体内容如下
效果图如下:
实现代码如下:
activity_popup_window.xml按钮
<?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:orientation="vertical"
tools:context=".PopupWindowActivity">
<Button
android:id="@+id/btn_popupWindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="PopupWindow" />
</LinearLayout>自定义弹出的视图layout_pop.xml,也可以用RecycleView或者ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_good"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="好"
android:textColor="@color/gray"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/gray" />
<TextView
android:id="@+id/tv_not_too_bad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="还行"
android:textColor="@color/gray"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/gray" />
<TextView
android:id="@+id/tv_bad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="不好"
android:textColor="@color/gray"
android:textSize="20sp" />
</LinearLayout>PopupWindowActivity类实现代码如下:
public class PopupWindowActivity extends AppCompatActivity {
private Button btn_popupWindow;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window);
btn_popupWindow = findViewById(R.id.btn_popupWindow);
btn_popupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View popup_view = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.layout_pop, null);
TextView textView = popup_view.findViewById(R.id.tv_good);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
Toast.makeText(PopupWindowActivity.this, "好", Toast.LENGTH_SHORT).show();
}
});
popupWindow = new PopupWindow(popup_view, btn_popupWindow.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
//设置弹出窗口应该接收外部触摸事件
popupWindow.setOutsideTouchable(true);
//设置可聚焦
popupWindow.setFocusable(true);
popupWindow.showAsDropDown(btn_popupWindow);
}
});
}
}以上就是PopupWindow弹出式窗口的简单使用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:Android中PopupWindow弹出式窗口使用方法详解
猜你喜欢
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- iOS 对当前webView进行截屏的方法 2023-03-01
- Flutter实现底部和顶部导航栏 2022-08-31
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Android studio实现动态背景页面 2023-05-23
- Android实现监听音量的变化 2023-03-30
- 详解flutter engine 那些没被释放的东西 2022-12-04
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- Android实现轮询的三种方式 2023-02-17
