【问题标题】:Can't set height and width of Popup Window无法设置弹出窗口的高度和宽度
【发布时间】:2017-03-28 11:30:38
【问题描述】:

我为弹出窗口做了一个布局,当我点击按钮显示弹出窗口时,它没有正确显示弹出对话框,只看到部分视图 这个

当我点击 3 个点时。

我希望我的弹出窗口像这样显示

当我点击 3 个点时。

这是我的代码。

 LayoutInflater layoutInflater
                            = (LayoutInflater)getBaseContext()
                            .getSystemService(LAYOUT_INFLATER_SERVICE);
                    final View popupView = layoutInflater.inflate(R.layout.custom_dialog, null);
                    final PopupWindow popupWindow = new PopupWindow(
                            popupView,
                            WindowManager.LayoutParams.WRAP_CONTENT,
                            WindowManager.LayoutParams.WRAP_CONTENT);
                    ImageButton gmail= (ImageButton) popupView.findViewById(R.id.gmail);
                    gmail.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v)
                        {
                            popupWindow.dismiss();
                        }
                    });

                    popupWindow.showAsDropDown(view.findViewById(R.id.more));

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"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">


<ImageButton
    android:id="@+id/gmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    app:srcCompat="@drawable/gmail"
    android:backgroundTint="#000000"
    android:layout_weight="0.25"/>

<ImageButton
    android:id="@+id/hike"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/hike"
    android:backgroundTint="#000000"
    android:scaleType="fitXY"
    android:layout_weight="0.25"/>

<ImageButton
    android:id="@+id/hangouts"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#000000"
    app:srcCompat="@drawable/hangouts"
    android:scaleType="fitXY"
    android:layout_weight=".25"/>

<ImageButton
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/message"
    android:scaleType="fitXY"
    android:backgroundTint="#000000"
    android:layout_weight=".25"/>
 </LinearLayout>

谁能告诉我哪里出错了? 提前致谢。 感谢大家的帮助

【问题讨论】:

    标签: android dialog popup android-5.0-lollipop


    【解决方案1】:

    替换 app:srcCompat="@drawable/hike"android:src="@drawable/hike"

    【讨论】:

    • 它显示错误:(11) 在包 'android' 中找不到属性 'srcCompat' 的资源标识符,这就是我使用 'app' 而不是 'android' 的原因
    • 使用 android:src 而不是 app:srcCompat,在我的情况下,我使用 android:src 工作正常。
    • 请在此处查看android-developers.googleblog.com/2016/02/…关于android支持库
    【解决方案2】:

    使用 setContentView

        PopupWindow popupWindow = new PopupWindow(popupView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
        popupWindow .setContentView(popupView);
    

    【讨论】:

    • 它也没有帮助。
    猜你喜欢
    • 2011-10-20
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多