我正在尝试创建一个弹出窗口,当启动时将位于此屏幕的中心,并且其宽度不会占据整个屏幕,无论提示它的按钮位于何处。我还想增加透明度,以便用户现在可以隐约看到后台活动,但我也遇到了使用 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)