我正在尝试创建一个弹出窗口,当启动位于此屏幕的中心时,其宽度不会占据整个屏幕,无论提示它的按钮位于何处。我还想添加透明度,以便用户能够在背景中模糊地看到活动,但我也遇到了使用alpha的问题。到目前为止,我的尝试并不令人满意。
特别是,当尝试在showAtLocation方法中使用重力时,会出现以下错误消息: “PopupWindow类型中的方法showAtLocation(View,int,int,int)不适用于参数”
非常感谢任何协助;提前谢谢。
以下是活动代码
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)
答案 0 :(得分:3)
我写的评论的详细答案
以下是showAtLocation()
方法的构造函数:
public void showAtLocation (View parent, int gravity, int x, int y);
如您所见,所需的第一个参数是View
。在您的代码中,您传递的是this
,在您的情况下,它是指Activity
。这就是你收到错误的原因。
您必须传递View
的父PopupWindow
。它基本上是您在View
方法中使用的setContentView
。
要修复它,你必须使用它:
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);