gpt4 book ai didi

设置 View 时Android警报对话框空白

转载 作者:太空狗 更新时间:2023-10-29 12:41:15 24 4
gpt4 key购买 nike

我有一个问题。我制作了一个警告对话框,显示一条消息并带有“不再显示”复选框的 View 。当我在 android KK 上运行这个应用程序时,它显示正常,但是当我在 android JB 上运行它时,它显示了一个空白区域,就像这张图片上的那样(当 View 只包含一个线性布局时,我也会得到这个空白区域0dp 的高度): http://foto.modelbouwforum.nl/images/2014/08/15/Screenshot2014-08-15-14-33-40.png

我的警告对话框代码:

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 上的样子: http://foto.modelbouwforum.nl/images/2014/08/15/QuickMemo2014-08-15-14-42-41.png

最佳答案

我查看了 API 19 中 alert_dialog.xml 布局的源代码,发现了以下用于插入自定义 View 的布局:

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

然后我尝试设置背景以查看哪个 View 占用了空间。 customPanel 为红色,自定义 View 为绿色。

AlertDialog d = builder.create();
d.show();

View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setBackgroundColor(Color.RED);

enter image description here

这张图片意味着 customPanel 应该有一些填充,或者 custom 应该有非零布局参数或其他东西。检查那些等于零的属性后,我尝试测试 customPanelminimumHeight 并在 xxhdpi 或 64dp 上得到 192。 此设置会导致框架大于嵌套的自定义 View

我还没有弄清楚这个设置在哪里应用,但这里是解决方法:

AlertDialog d = builder.create();
d.show();

View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setMinimumHeight(0);

在 API 18,19 上测试。

关于设置 View 时Android警报对话框空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25326665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com