Android的定制AlertDialog高度没有履行布局参数 [英] Android custom AlertDialog height not honoring layout parameters
问题描述
所以我创建一个自定义AlertDialog使用Builder。我有我的对话框与以下布局膨胀的自定义视图:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT> < EditText上机器人:ID =@ + ID / edit_username
风格=@风格/ EditPassword
机器人:提示=@字符串/ login_username_hint/> < EditText上机器人:ID =@ + ID / edit_password
风格=@风格/ EditPassword
机器人:提示=@字符串/ login_password_hint/>< / LinearLayout中>
在Android:为控制的EditText风格layout_height设置为WRAP_CONTENT。当我表明与此自定义对话框查看对话框被拉伸以填充整个屏幕的高度。不管我怎么设置(包括硬codeD像素值)上的LinearLayout的layout_height,这还罢了,在我的模拟器整个屏幕。
我希望有一些简单的,我在这里丢失?
编辑:我看着在层次浏览器,我包括在这个问题上的布局是正确定义,但它是一个的FrameLayout内的FrameLayout内包装,和最外层的FrameLayout设置为WRAP_CONTENT,但在浏览器中渲染带着一帮下方空。
编辑2:按照要求,code,它膨胀的布局
保护对话框onCreateDialog(INT ID){
开关(ID){
案例AUTHENTICATION_DIALOG:
LayoutInflater吹气=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
最后查看loginView = inflater.inflate(R.layout.login_dialog,NULL);
返回新AlertDialog.Builder(HomeActivity.this)
.setTitle(上传文件数据)
.setView(loginView)
.setPositiveButton(上传,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
userName的的EditText =(EditText上)loginView.findViewById(R.id.edit_username);
的EditText密码=(EditText上)loginView.findViewById(R.id.edit_password);
串userNameStr = StringUtils.convertToTrimmedString(userName.getText());
串passwordStr = StringUtils.convertToTrimmedString(password.getText()); 如果(userNameStr.equals()|| passwordStr.equals()){
新AlertDialog.Builder(HomeActivity.this)
.setTitle(必填项失踪)
.setMessage(您必须输入用户名和密码)
.setPositiveButton(OK,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
HomeActivity.this.showDialog(AUTHENTICATION_DIALOG);
dialog.dismiss();
}
})。显示();
}其他{
dialog.dismiss();
} // end如果用户输入的用户名和密码
} //结束上传的onClick
})//结束setPositiveButton DialogInterface.OnClickListener()
.setNegativeButton(取消,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
dialog.dismiss();
} //结束取消的onClick
})。创建();
默认:
返回null;
}
}
于是我转而使用一个RelativeLayout的,像这样:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT> < EditText上机器人:ID =@ + ID / edit_username
风格=@风格/ EditPassword
机器人:提示=@字符串/ login_username_hint/> < EditText上机器人:ID =@ + ID / edit_password
风格=@风格/ EditPassword
机器人:layout_below =@ + ID / edit_username
机器人:提示=@字符串/ login_password_hint/>< / RelativeLayout的>
和它工作正常。我换回到线性测试,并展出了破旧的行为,当我切换回的LinearLayout。我要离开这个答案在无人盯防的希望有人能告诉我,为什么它吓坏了,过度的LinearLayout VS相对的。
So I am creating a custom AlertDialog using the Builder. I have a custom view I am inflating in the dialog with the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText android:id="@+id/edit_username"
style="@style/EditPassword"
android:hint="@string/login_username_hint" />
<EditText android:id="@+id/edit_password"
style="@style/EditPassword"
android:hint="@string/login_password_hint" />
</LinearLayout>
The android:layout_height style for the EditText controls is set to "wrap_content". When I show the dialog with this custom view the dialog is stretched to fill the height of the entire screen. No matter what I set the layout_height on the LinearLayout to (including hardcoded pixel values), it still fills the entire screen on my emulator.
I'm hoping there's something simple that I'm missing here?
EDIT: I looked in the Hierarchy Viewer and the layout I included in this question is correctly defined, but it is wrapped within a FrameLayout within a FrameLayout, and the outermost FrameLayout is set to "wrap_content" but is rendering in the Viewer with a bunch of empty space below it.
EDIT 2: As requested, the code that inflates the layout.
protected Dialog onCreateDialog(int id) {
switch(id) {
case AUTHENTICATION_DIALOG:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
final View loginView = inflater.inflate(R.layout.login_dialog, null);
return new AlertDialog.Builder(HomeActivity.this)
.setTitle("Upload profile data")
.setView(loginView)
.setPositiveButton("Upload", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText userName = (EditText)loginView.findViewById(R.id.edit_username);
EditText password = (EditText)loginView.findViewById(R.id.edit_password);
String userNameStr = StringUtils.convertToTrimmedString(userName.getText());
String passwordStr = StringUtils.convertToTrimmedString(password.getText());
if (userNameStr.equals("") || passwordStr.equals("")) {
new AlertDialog.Builder(HomeActivity.this)
.setTitle("Required fields missing")
.setMessage("You must enter a username and password")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
HomeActivity.this.showDialog(AUTHENTICATION_DIALOG);
dialog.dismiss();
}
}).show();
} else {
dialog.dismiss();
} // end if user entered username and password
} // end "Upload" onClick
}) // end setPositiveButton DialogInterface.OnClickListener()
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
} // end "Cancel" onClick
}).create();
default:
return null;
}
}
So I switched to using a RelativeLayout like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText android:id="@+id/edit_username"
style="@style/EditPassword"
android:hint="@string/login_username_hint" />
<EditText android:id="@+id/edit_password"
style="@style/EditPassword"
android:layout_below="@+id/edit_username"
android:hint="@string/login_password_hint" />
</RelativeLayout>
and it works fine. I switched back to Linear to test and it exhibited the old broken behavior when I switched back to the LinearLayout. I'm going to leave this answer unmarked in the hopes that someone can tell me why it's freaking out over LinearLayout vs Relative.
这篇关于Android的定制AlertDialog高度没有履行布局参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!