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

时间:2016-02-25 18:00:06

标签: java android android-studio

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

问题是,当创建弹出窗口时,它是一个小盒子。像100x100或其他东西。这是弹出代码。你可以看到我尝试设置550和750,作为大小,如果我把70和550,它将完美地工作,并且是非常小的宽度,并且长期明智地伸展。但是在100dp之后,它会回到正方形

  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:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:background="@drawable/blank">

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

1 个答案:

答案 0 :(得分:0)

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

尝试使用match_parent