我有一個彈出窗口的問題。我想用我自己的佈局創建彈出窗口。 這是代碼:設置自己的佈局在彈出的窗口中的Android
public class PopupWindowView extends PopupWindow{
PopupWindow popup;
boolean click = true;
LayoutParams params;
RelativeLayout mainLayout;
TextView tv;
LinearLayout layout;
ImageView chooseFlag;
public void createPopupWindow(Activity act){
popup = new PopupWindow(act);
chooseFlag = (ImageView) act.findViewById(R.id.login_choose_flag);
mainLayout = (RelativeLayout) act.findViewById(R.id.login_layout);
tv = new TextView(act);
layout = new LinearLayout(act);
//layout = (LinearLayout) findViewById(R.id.popuplayout);
chooseFlag.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (click) {
int[] values = new int[2];
v.getLocationOnScreen(values);
popup.showAtLocation(mainLayout, Gravity.NO_GRAVITY, 10, 10);
popup.update(values[0], values[1], 300, 80);
click = false;
} else {
popup.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popup.setContentView(layout);
}
}
,這是佈局,我想在我的彈出窗口設置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/popuplayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/patient_button_bg">
</LinearLayout>
</LinearLayout>
在我的課,我不能使用findbyid
方法,因爲這不是活動。我如何在我的課堂中設置我自己的彈出窗口中的佈局?
編輯: 這是堆棧跟蹤在那裏我得到錯誤:其中使用彈出
03-01 09:48:48.761: E/AndroidRuntime(16776): FATAL EXCEPTION: main
03-01 09:48:48.761: E/AndroidRuntime(16776): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.view.ViewGroup.addView(ViewGroup.java:3208)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.view.ViewGroup.addView(ViewGroup.java:3188)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.widget.PopupWindow.preparePopup(PopupWindow.java:969)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:840)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:813)
03-01 09:48:48.761: E/AndroidRuntime(16776): at pl.asseco.amms.mobile.tools.PopupWindowView$1.onClick(PopupWindowView.java:44)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.view.View.performClick(View.java:3558)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.view.View$PerformClick.run(View.java:14152)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.os.Handler.handleCallback(Handler.java:605)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.os.Looper.loop(Looper.java:137)
03-01 09:48:48.761: E/AndroidRuntime(16776): at android.app.ActivityThread.main(ActivityThread.java:4514)
03-01 09:48:48.761: E/AndroidRuntime(16776): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 09:48:48.761: E/AndroidRuntime(16776): at java.lang.reflect.Method.invoke(Method.java:511)
03-01 09:48:48.761: E/AndroidRuntime(16776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-01 09:48:48.761: E/AndroidRuntime(16776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-01 09:48:48.761: E/AndroidRuntime(16776): at dalvik.system.NativeStart.main(Native Method)
編輯 活動:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getActionBar().hide();
mainMenuGenerator = new MainMenuGenerator();
mainMenuGenerator.generateMainMenu(this);
mainMenuGenerator.hideIcons();
popup = new PopupWindowView();
popup.createPopupWindow(this);
}
有用,謝謝! '清除默認的半透明背景',它將我拉上了牆(尤其是沒有發現我的佈局造成了什麼) – YvesLeBorg 2017-04-18 15:51:11