Android - 以编程方式更改Popup Window的布局

时间:2016-01-13 10:02:15

标签: android android-layout

我有PopupWindow最终决定遵循我的命令并在出现null错误后显示自己(当然不是我忘记初始化某些值时的错误)。

这是PopupWindow

的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_handphone_MainLayout"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/popup_handphone_Wrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/popupkodehp">

        <TextView
            android:id="@+id/popup_handphone_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:textColor="@color/textColor"
            android:text="@string/popupPhoneMessage"/>

        <RelativeLayout
            android:id="@+id/popup_handphone_functionalities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/popup_handphone_text"
            android:layout_marginTop="15dp">

            <EditText
                android:id="@+id/popup_handphone_phoneNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Phone Number"
                android:textColorHint="@color/textColor" />

            <EditText
                android:id="@+id/popup_handphone_phoneNumberConfirm"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/popup_handphone_phoneNumber"
                android:layout_marginTop="10dp"
                android:hint="Retype Phone Number"
                android:textColorHint="@color/textColor"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/popup_handphone_phoneNumberConfirm"
                android:layout_marginTop="20dp"
                android:minHeight="20dp"
                android:text="Send Code"
                android:textColor="@color/textColor"/>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

以下是我在谷歌匿名和令人惊叹的人的帮助下制作的PopupWindow代码

View.OnClickListener phoneReinputHandler = new View.OnClickListener() {

        public void onClick(View arg0) {
            /*Intent intent = new Intent(SignupStepTwoActivity.this, PopupHandphone.class);
            backDim = (RelativeLayout) findViewById(R.id.bac_dim_layout);
            //backDim.setVisibility(View.VISIBLE);
            startActivity(intent);*/

            mainLayout = (RelativeLayout)findViewById(R.id.activity_signup_step_two_mainLayout);

            int width = displayMetrics.widthPixels;
            int height = displayMetrics.heightPixels;
            inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*            View popupLayoutInflater = inflater.inflate(R.layout.popup_handphone, mainLayout);

            RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)popupLayoutInflater.findViewById(R.id.popup_handphone_functionalities);
            int popupFunctionalitiesWrapperHeight = layoutResize.height(70, displayMetrics);
            RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams();
            popupFunctionalitiesWrapperParams.height = popupFunctionalitiesWrapperHeight;
*/
            PopupWindow pw = new PopupWindow(
                    inflater.inflate(R.layout.popup_handphone, null, false),
                    (int)(width * .8),
                    (int)(height*.35),
                    true);
            pw.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
            pw.setOutsideTouchable(true);

            pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

        }
    };

第二个评论部分是尝试设置布局元素的高度失败。它也显示在主要布局上,因为整个PopupWindow布局位于类的布局之上。

如何以编程方式自定义我从类'布局中制作的PopupWindow布局?

2 个答案:

答案 0 :(得分:0)

如果要在弹出窗口显示时更新PopupWindow宽度和高度,请使用函数PopupWindow.update

希望这有帮助。

答案 1 :(得分:0)

我找到了关于如何使用PopupWindow更改popupWindow.getContentView()内部的答案

像往常一样,实例化PopupWindow

PopupWindow pw = new PopupWindow(
                    inflater.inflate(R.layout.popup_handphone, null, false),
                    (int)(width * .8),
                    (int)(height*.35),
                    true);

然后使用..

调用PopupWindow的布局
RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)pw.getContentView().findViewById(R.id.popup_handphone_functionalities);

请注意,pw.getContentView()之前有findViewById

通过popupFunctionalitiesWrapper,我可以设置像......这样的东西。

int popupFunctionalitiesWrapperWidth = layoutResize.width(70);
            RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams();
            popupFunctionalitiesWrapperParams.width = popupFunctionalitiesWrapperWidth;
            popupFunctionalitiesWrapperParams.addRule(Gravity.CENTER);
            popupFunctionalitiesWrapper.setLayoutParams(popupFunctionalitiesWrapperParams);
是的,那就是那个!