【问题标题】:Set custom layout in popup window in android在android的弹出窗口中设置自定义布局
【发布时间】:2019-11-25 16:49:41
【问题描述】:

弹出窗口有问题。我想用我自己的布局创建弹出窗口。 这是代码:

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);

}

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    试试这个代码:

    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();
             }
           });
    
    }
    

    【讨论】:

    • 有用,谢谢! clear the default translucent background,它把我逼到了墙角(尤其是没有找到我的布局中的原因)
    【解决方案2】:

    您可以使用以下代码。您需要为此使用 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(); 关闭 PopupWindow

    【讨论】:

      【解决方案3】:

      您可以使用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);
      

      【讨论】:

      • 我使用了你的解决方案,但我得到错误:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。
      • 根据例外情况,您似乎正在尝试将之前已添加到不同布局的视图添加到其他布局中,请问您在哪一行收到此异常?
      • 您不能调试并查看它在哪个特定行引发了此错误吗?从堆栈跟踪中,您会在 preparePopup 方法中收到此错误
      • 好的,当我运行应用程序调试器时,一切正常。但是当我点击这个按钮时,我得到错误并且调试器停止。它看起来像第一次在 Activity 中的 onCreate 中创建方法,当您单击按钮时出现错误。我从活动粘贴了 onCreate
      • 您的意思是它在从 onCreate 调用时有效,但稍后单击按钮时不会?您的按钮点击事件的来源在哪里?
      【解决方案4】:

      试试这个……

      public void popUpWindow() {
          final Dialog dialog = new Dialog(MainActivity.this);
          dialog.setContentView(R.layout.client_details);
          dialog.setTitle("Client Details");
          dialog.show();
      }
      

      【讨论】:

        【解决方案5】:

        您好,请检查此 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);
           }
         }
        

        【讨论】:

        • 我不想在活动中这样做。我想做自己的控制来扩展 PopUpWindow
        【解决方案6】:
            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();
                }
        

        【讨论】:

        • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-24
        相关资源
        最近更新 更多