gpt4 book ai didi

java - 使用 PopupWindow 的 showAtLocation(View, int, int, int) 时遇到问题

转载 作者:行者123 更新时间:2023-12-01 22:48:40 25 4
gpt4 key购买 nike

我正在尝试创建一个弹出窗口,启动时该弹出窗口将位于该屏幕的中心,并且其宽度不会占据整个屏幕,无论提示它的按钮位于何处。我还想增加透明度,以便用户能够模糊地看到现在在后台的 Activity ,但我也遇到了使用 alpha 的问题。到目前为止我的尝试并不令人满意。

特别是,当尝试在 showAtLocation 方法中使用重力时,系统会提示我以下错误消息:“PopupWindow 类型中的方法 showAtLocation(View, int, int, int) 不适用于该参数”

如有任何帮助,我们将不胜感激;提前致谢。

下面是 Activity 代码

import android.app.Activity; 
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {
LinearLayout popupLayout;
PopupWindow popupMessage;
Button popupButton, insidePopupButton;
TextView popupText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); init(); popupInit(); }
public void init() { popupButton = (Button) findViewById(R.id.popupbutton);

popupText = new TextView(this);
insidePopupButton = new Button(this);
popupLayout = new LinearLayout(this);
popupLayout.setBackgroundColor(Color.parseColor("#dd000000"));
popupLayout.setGravity(Gravity.CENTER);

insidePopupButton.setText("OK");
popupText.setText("This is Popup Window.press OK to dismiss it.");
popupText.setPadding(30, 40, 30, 20);
popupLayout.setOrientation(1);
popupLayout.addView(popupText);
popupLayout.addView(insidePopupButton);
}
public void popupInit() {
popupButton.setOnClickListener(this);
insidePopupButton.setOnClickListener(this);
popupMessage = new PopupWindow(popupLayout, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
popupMessage.setContentView(popupLayout);
popupMessage.showAtLocation(this, Gravity.CENTER, 0, 0);


}
@Override public void onClick(View v) {
if (v.getId() == R.id.popupbutton) {
popupMessage.showAsDropDown(popupButton, 0, 0);
}
else {
popupMessage.dismiss();
} } }

下面是xml布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="good day, click button below to see popup" />

<Button
android:id="@+id/popupbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Show Popup" />

</RelativeLayout>

更新了代码

/**
* Copyright 2010-present Facebook.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.samples.friendpicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class PopupDemoActivity extends Activity implements OnClickListener {
LinearLayout layoutOfPopup;
PopupWindow popupMessage;
Button popupButton, insidePopupButton;
TextView popupText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); init(); popupInit(); }
public void init() { popupButton = (Button) findViewById(R.id.popupbutton);
popupText = new TextView(this);
insidePopupButton = new Button(this);
layoutOfPopup = new LinearLayout(this);
insidePopupButton.setText("OK");
popupText.setText("This is Popup Window.press OK to dismiss it.");
popupText.setPadding(0, 0, 0, 20);
layoutOfPopup.setOrientation(1);
layoutOfPopup.addView(popupText);
layoutOfPopup.addView(insidePopupButton);
}
public void popupInit() {
popupButton.setOnClickListener(this);
insidePopupButton.setOnClickListener(this);
View parent = findViewById(R.layout.main);
popupMessage = new PopupWindow(layoutOfPopup, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
popupMessage.setContentView(layoutOfPopup);
popupMessage.showAtLocation(parent, Gravity.CENTER, 0, 0);
}
@Override public void onClick(View v) {
if (v.getId() == R.id.popupbutton) {
popupMessage.showAsDropDown(popupButton, 0, 0);

}
else {
popupMessage.dismiss();
} } }

来自 logcat 的消息

07-29 12:36:19.761: E/AndroidRuntime(1720): FATAL EXCEPTION: main
07-29 12:36:19.761: E/AndroidRuntime(1720): Process: com.example.popuptest, PID: 1720
07-29 12:36:19.761: E/AndroidRuntime(1720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.popuptest/com.example.popuptest.PopupDemoActivity}: java.lang.NullPointerException
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.os.Handler.dispatchMessage(Handler.java:102)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.os.Looper.loop(Looper.java:136)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-29 12:36:19.761: E/AndroidRuntime(1720): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 12:36:19.761: E/AndroidRuntime(1720): at java.lang.reflect.Method.invoke(Method.java:515)
07-29 12:36:19.761: E/AndroidRuntime(1720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-29 12:36:19.761: E/AndroidRuntime(1720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-29 12:36:19.761: E/AndroidRuntime(1720): at dalvik.system.NativeStart.main(Native Method)
07-29 12:36:19.761: E/AndroidRuntime(1720): Caused by: java.lang.NullPointerException
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:814)
07-29 12:36:19.761: E/AndroidRuntime(1720): at com.example.popuptest.PopupDemoActivity.popupInit(PopupDemoActivity.java:41)
07-29 12:36:19.761: E/AndroidRuntime(1720): at com.example.popuptest.PopupDemoActivity.onCreate(PopupDemoActivity.java:23)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.app.Activity.performCreate(Activity.java:5231)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-29 12:36:19.761: E/AndroidRuntime(1720): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

最佳答案

我写的评论的详细答案

<小时/>

这是 showAtLocation() 方法的构造函数:

public void showAtLocation (View parent, int gravity, int x, int y);

如您所见,所需的第一个参数是View。在您的代码中,您传递的是 this,在您的情况下,它指的是 Activity。这就是您收到错误的原因。

您必须传递 PopupWindow 的父 View。它基本上是您在 setContentView 方法中使用的 View

要修复它,你必须使用这个:

View parent = findViewById(R.layout.activity_main);
popupMessage = new PopupWindow(popupLayout, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
popupMessage.setContentView(popupLayout);
popupMessage.showAtLocation(parent, Gravity.CENTER, 0, 0);

关于java - 使用 PopupWindow 的 showAtLocation(View, int, int, int) 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24997930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com