有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么这个弹出窗口只显示为一个小正方形?

所以我制作了一个简单的应用程序,你点击一个按钮,它会显示一个带有随机字符串的弹出窗口

问题是,创建弹出窗口时,它是一个小框。像100x100之类的。这是弹出代码。你可以看到,我试着设置550和750,作为尺寸,如果我设置70和550,它会工作得很好,而且宽度很小,而且拉伸得很长。但在100分钟后,它又回到了正方形

  public void showPopup(View v){

        View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);

        PopupWindow popupWindow = new PopupWindow(popupView, 550, 750);

        // Example: If you have a TextView inside `popup_layout.xml`
        TextView tv = (TextView) popupView.findViewById(R.id.tv);

        String[] myString;
        Resources res = getResources();
        myString = res.getStringArray(R.array.myArray);

        String q = myString[rgenerator.nextInt(myString.length)];
        tv.setText(q);



        // Initialize more widgets from `popup_layout.xml`


        // If the PopupWindow should be focusable
        popupWindow.setFocusable(true);

        // If you need the PopupWindow to dismiss when when touched outside
        popupWindow.setBackgroundDrawable(new ColorDrawable());



        // Get the View's(the one that was clicked in the Fragment) location
       //v.getLocationOnScreen(location);

        // Using location, the PopupWindow will be displayed right under anchorView
        popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
    }

这是我的弹出式布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content"
    安卓:background="@drawable/blank">

    <TextView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:textAppearance="?安卓:attr/textAppearanceLarge"
        安卓:id="@+id/tv"
        安卓:layout_alignParentTop="true"
        安卓:layout_alignParentRight="true"
        安卓:layout_alignParentEnd="true"
        安卓:textColor="#FFFF"
        安卓:layout_gravity="center" />
</FrameLayout>

共 (1) 个答案

  1. # 1 楼答案

    android:layout_width="wrap_content" android:layout_height="wrap_content"
    

    试试match_parent