posts - 296,comments - 1,views - 2976

一.今天做了什么

PopupWindow(悬浮框)

常用方法
setContentView(View contentView):设置PopupWindow显示的view
showAsDropDown(View anchor):相对某个控件的位置〈正左下方),无偏移showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
setFocusable(boolean focusable):设置是否获取焦点
setBackgroundDrawable(Drawable background):设置背景dismiss()关闭弹窗
setAnimationStyle(int animationStyle):设置加载动画
setTouchable(boolean touchable):设置触摸使能
setOutsideTouchable(boolean touchable):设置PopupWindow外面的触摸使能
更多详见:2.6.1 PopupWindow(悬浮框)的基本使用 | 菜鸟教程 (runoob.com)

XML代码:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="弹出PopupWIndow"
    android:onClick="leoClick"/>
popup_view.xml代码:

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="上海"
    android:textSize="18sp"/>

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:text="北京"
    android:textSize="18sp"/>

Java代码:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "leo";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void leoClick(View view) {
    View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
    //视图、宽、高、是否获取焦点(点击空白处取消)
    PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
    popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ceshi)); //设置背景
    popupWindow.showAsDropDown(view, 50,0);
    Log.e(TAG, "leoClick: 111111");

    Button btn1 = popupView.findViewById(R.id.btn1);
    Button btn2 = popupView.findViewById(R.id.btn2);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e(TAG, "onClick: 按钮1被点击");
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e(TAG, "onClick: 按钮2被点击");
        }
    });
}

}

posted on   leapss  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示