Android-通过编程更改弹出窗口';s布局
我有一个Android-通过编程更改弹出窗口';s布局,android,android-layout,Android,Android Layout,我有一个PopupWindow,它最终决定遵循我的命令并在出现null错误后显示自己(当然不是我忘记初始化某个值的错误) 这是用于PopupWindow <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/popup_handphone_MainLayout"
PopupWindow
,它最终决定遵循我的命令并在出现null错误后显示自己(当然不是我忘记初始化某个值的错误)
这是用于PopupWindow
<?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 pw = new PopupWindow(
inflater.inflate(R.layout.popup_handphone, null, false),
(int)(width * .8),
(int)(height*.35),
true);
第二个注释部分是设置布局元素高度的失败尝试。它也显示在主布局上,如中所示,整个PopupWindow
布局位于类布局的顶部
如何以编程方式自定义我从类“layout I'm at?”制作的
PopupWindow
布局如果要在显示PopupWindow时更新PopupWindow
宽度和高度,请使用函数PopupWindow.update
希望这能有所帮助。我找到了如何使用
PopupWindow.getContentView()更改PopupWindow
内部的答案。
像往常一样,实例化PopupWindow
<?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 pw = new PopupWindow(
inflater.inflate(R.layout.popup_handphone, null, false),
(int)(width * .8),
(int)(height*.35),
true);
然后使用调用弹出窗口的布局
RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)pw.getContentView().findViewById(R.id.popup_handphone_functionalities);
请注意,pw.getContentView()
位于findViewById
之前
通过这个popupFunctionatiesWrapper
,我可以设置像
int popupFunctionalitiesWrapperWidth = layoutResize.width(70);
RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams();
popupFunctionalitiesWrapperParams.width = popupFunctionalitiesWrapperWidth;
popupFunctionalitiesWrapperParams.addRule(Gravity.CENTER);
popupFunctionalitiesWrapper.setLayoutParams(popupFunctionalitiesWrapperParams);
是的,就是这样 那是给窗户的吗?或者它可以用于内部的单个组件??如果要重置宽度
和高度
然后PopupWindow
对象,或者如果要更新内部的单个组件,则通过PopupWindow.getContentView从PopupWindow获取视图,然后更新