2013-03-01 63 views
15

我有一個彈出窗口的問題。我想用我自己的佈局創建彈出窗口。 這是代碼:設置自己的佈局在彈出的窗口中的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); 

} 

回答

20

試試這個代碼:

private void showSortPopup(final Activity context, Point p) 
{ 
     // Inflate the popup_layout.xml 
     LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llSortChangePopup); 
     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = layoutInflater.inflate(R.layout.sort_popup_layout, viewGroup); 

     // Creating the PopupWindow 
     changeSortPopUp = new PopupWindow(context); 
     changeSortPopUp.setContentView(layout); 
     changeSortPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT); 
     changeSortPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT); 
     changeSortPopUp.setFocusable(true); 

     // Some offset to align the popup a bit to the left, and a bit down, relative to button's position. 
     int OFFSET_X = -20; 
     int OFFSET_Y = 95; 

     // Clear the default translucent background 
     changeSortPopUp.setBackgroundDrawable(new BitmapDrawable()); 

     // Displaying the popup at the specified location, + offsets. 
     changeSortPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y); 


     // Getting a reference to Close button, and close the popup when clicked. 
     Button close = (Button) layout.findViewById(R.id.close); 
     close.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      changeSortPopUp.dismiss(); 
     } 
     }); 

} 
+1

有用,謝謝! '清除默認的半透明背景',它將我拉上了牆(尤其是沒有發現我的佈局造成了什麼) – YvesLeBorg 2017-04-18 15:51:11

1

嗨檢查這個post包括解決方案,以你的問題

試試這個代碼:

public class ShowPopUp extends Activity { 

PopupWindow popUp; 
LinearLayout layout; 
TextView tv; 
LayoutParams params; 
LinearLayout mainLayout; 
Button but; 
boolean click = true; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    popUp = new PopupWindow(this); 
    layout = new LinearLayout(this); 
    mainLayout = new LinearLayout(this); 
    tv = new TextView(this); 
    but = new Button(this); 
    but.setText("Click Me"); 
    but.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      if (click) { 
       popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10); 
       popUp.update(50, 50, 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); 
    // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10); 
    mainLayout.addView(but, params); 
    setContentView(mainLayout); 
    } 
} 
+0

我不要;不想做,在活動。我想做自己的控制,它擴展了PopUpWindow – user1302569 2013-03-01 10:10:23

4

您可以使用LayoutInflater

LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.custom_layout, null); //custom_layout is your xml file which contains popuplayout 
LinearLayout layout = (LinearLayout) view.findViewById(R.id.popuplayout); 
+0

我用你的解決方案,但我得到錯誤:java.lang.IllegalStateException:指定的子項已經有一個父項。您必須先調用子對象的父對象的removeView()。 – user1302569 2013-03-01 08:50:47

+0

根據例外情況,您似乎嘗試將以前添加到不同佈局的視圖添加到視圖中,請問您在哪一行獲取此異常? – Waqas 2013-03-01 09:41:03

+0

難道你不能調試,看看在哪個特定的行它thorws這個錯誤?從堆棧跟蹤中,您在preparePopup方法 – Waqas 2013-03-01 10:13:18

5

您可以使用下面的代碼。你需要爲此使用PopupWindow。

PopupWindow mpopup; 

然後你需要膨脹你的看法。

View popUpView = getLayoutInflater().inflate(R.layout.activity_login, 
      null); // inflating popup layout 
    mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT, true); // Creation of popup 
    mpopup.setAnimationStyle(android.R.style.Animation_Dialog); 
    mpopup.showAtLocation(popUpView, Gravity.CENTER, 0, 0); // Displaying popup 

如果您的佈局有一些項目,那麼您需要將該項目與您的視圖綁定。

TextView some = (TextView) popUpView.findViewById(R.id.some);  
    Button btnCancel = (Button) popUpView.findViewById(R.id.btnCancel); 

onClickListener您的彈出窗口項目。

btnCancel.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mpopup.dismiss(); 
     } 
    }); 

可以使用mpopup.dismiss();

1

試試這個解散你PopupWindow ......

public void popUpWindow() { 
    final Dialog dialog = new Dialog(MainActivity.this); 
    dialog.setContentView(R.layout.client_details); 
    dialog.setTitle("Client Details"); 
    dialog.show(); 
} 
0
alert=(Button)findViewById(R.id.click); 
    detail=(ImageView)findViewById(R.id.goDetail); 

    alert.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      // custom dialog 

      final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.custom); 
      dialog.setTitle(Html.fromHtml("<font color='#EC407A'>About</font>")); 
      Button dialogButton = (Button) dialog.findViewById(R.id.dialogOK); 
      ImageView img = (ImageView) dialog.findViewById(R.id.goDetail); 
      // if button is clicked, close the custom dialog 
      dialogButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        dialog.dismiss(); 
        Toast.makeText(getApplicationContext(),"Dismissed..!!",Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      img.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        i=new Intent(getApplication(),CustomDetail.class); 
        setIntent(i); 
        Toast.makeText(Dashboard.this, "Check detail ", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      dialog.show(); 
     } 
+0

儘管這段代碼片段可以解決這個問題,[包括解釋](http://meta.stackexchange .com/questions/114762/explain-completely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – 2017-03-31 08:32:16