设置视图时Android警报对话框空白
我有个问题。我创建了一个警报对话框,该对话框显示消息,并带有“不再显示”复选框。当我在android KK上运行此应用程序时,它显示良好,但当我在android JB上运行时,它会显示一个类似于此图像的空白区域(当视图仅包含高度为0dp的线性布局时,我也会获得此空白区域): 警报对话框的我的代码:设置视图时Android警报对话框空白,android,android-alertdialog,Android,Android Alertdialog,我有个问题。我创建了一个警报对话框,该对话框显示消息,并带有“不再显示”复选框。当我在android KK上运行此应用程序时,它显示良好,但当我在android JB上运行时,它会显示一个类似于此图像的空白区域(当视图仅包含高度为0dp的线性布局时,我也会获得此空白区域): 警报对话框的我的代码: final SharedPreferences sharedPreferences=getSharedPreferences("appPrefs", Context.MODE_PRIVATE);
final SharedPreferences sharedPreferences=getSharedPreferences("appPrefs", Context.MODE_PRIVATE);
if(!sharedPreferences.getBoolean("isTutorialMainScreenChecked", false)){
View checkBoxView = View.inflate(MainScreenHandler.this, R.layout.checkbox_alert_dialog,null);
checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.alertDialog_main_screen_tutorial));
builder.setCancelable(false);
builder.setView(checkBoxView);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(checkBox.isChecked()){
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("isTutorialMainScreenChecked", true);
editor.commit();
}
dialogInterface.dismiss();
}
});
builder.setNegativeButton(getString(R.string.alertDialog_cancel),
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialogInterface, int i){
dialogInterface.cancel();
}
});
alertDialog = builder.create();
alertDialog.show();
这是我的复选框布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:baselineAligned="false">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffff3bb2"/>
<CheckBox
style="@android:style/TextAppearance.Small"
android:textColor="#ff000000"
android:text="@string/alertDialog_checkbox_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox"/>
</LinearLayout>
提前谢谢
附言:
这是android KK上的外观:
你有没有尝试过使用填充物和边距?
您可以在res/layout vXX中复制布局并设置特定(负)边距。我在API 19中查看了
alert\u dialog.xml
layout的来源,找到了以下用于插入自定义视图的布局:
<FrameLayout android:id="@+id/customPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<FrameLayout android:id="@+android:id/custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip" />
</FrameLayout>
此图片表示customPanel
应具有一些填充,或者custom
应具有非零布局参数或其他内容。在检查那些等于零的属性之后,我尝试测试customPanel
的minimumHeight
,在xxhdpi或64dp上得到192。此设置导致框架大于嵌套的自定义视图
我还没有弄清楚此设置应用于何处,但以下是解决方法:
AlertDialog d = builder.create();
d.show();
View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setMinimumHeight(0);
根据API 18,19进行测试。除vokilam的回答外: 如果您使用的是android.support.v7.app.AlertDialog 在API 27上测试(非常确定这也适用于API 28) 布局:
<FrameLayout
android:id="@+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Space
android:id="@+id/textSpacerNoTitle"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="@dimen/dialog_padding_top_material" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="?attr/dialogPreferredPadding"
android:paddingStart="?attr/dialogPreferredPadding"
style="@style/TextAppearance.Material.Subhead" />
<Space
android:id="@+id/textSpacerNoButtons"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="@dimen/dialog_padding_top_material" />
</LinearLayout>
</ScrollView>
</FrameLayout>
<FrameLayout
android:id="@+id/customPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
<FrameLayout
android:id="@+id/custom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
结果:
如果空白部分不是视图的一部分,那么我是否设置了一个负边距并不重要。正如我所说的,当视图高度为0DIP时,空白空间(同样高度)也有。奇怪的是,他们指定了该布局的最小高度!在过去的一个小时里,我一直在为这个问题绞尽脑汁,这个技巧奏效了,谢谢!
<FrameLayout
android:id="@+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Space
android:id="@+id/textSpacerNoTitle"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="@dimen/dialog_padding_top_material" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="?attr/dialogPreferredPadding"
android:paddingStart="?attr/dialogPreferredPadding"
style="@style/TextAppearance.Material.Subhead" />
<Space
android:id="@+id/textSpacerNoButtons"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="@dimen/dialog_padding_top_material" />
</LinearLayout>
</ScrollView>
</FrameLayout>
<FrameLayout
android:id="@+id/customPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
<FrameLayout
android:id="@+id/custom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
alertDialog.SetView(view);
alertDialog.show();
...
View customPanel = (View) view.getParent().getParent();
if (customPanel != null)
customPanel.setMinimumHeight(0);
View contentPanel = (View) alertDialog.findViewById(android.R.id.message).getParent().getParent().getParent();
if (contentPanel != null)
contentPanel.setMinimumHeight(contentPanel.getMinimumHeight() * 2 / 3);