我正在try 在Android中生成一个自定义对话框.

dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);

除了对话框的标题外,其他一切都很好.

有什么方法可以隐藏对话框的这一部分吗?

我用AlertDialog试过,但布局似乎设置不正确:

LayoutInflater inflater = 
    (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.map_dialog, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);

// dialog = new Dialog(this);
// dialog.setContentView(R.layout.map_dialog);

dialog = builder.create();

((TextView) dialog.findViewById(R.id.nr)).setText(number);

如果我使用这段代码,我会在最后一行得到一个空指针异常.该对话框不为空,因此我try 检索的文本视图不存在

推荐答案

您可以使用以下命令隐藏对话框标题:

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


这个答案的前一个版本过于复杂:

你需要使用AlertDialog.Android开发者的网站上有一个很好的解释,大约有custom dialogs个.

在非常简短的总结中,您可以使用下面从官方网站复制的代码来实现这一点.这将获取一个自定义布局文件,将其inflating ,给它一些基本文本和图标,然后创建它.那你就拿alertDialog.show()块来展示吧.

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
        mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
        (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

回应 comments :

我假设id为nr的TextView在您正在使用View view = inflater....进行充气的视图中.如果是这样,那么你只需要改变一点:将dialog.findView...改为view.findView....一旦你做到了,记得使用对话框.show(),甚至是builder.show()不必费心go 做.创建().

Android相关问答推荐

ENV变量在gradle进程中没有更新

Jetpack编写Lazy列滑动删除动画不顺利结束

显示本地房间数据库中未保存的项目的动态列表

如何go 除回调流中不可用的状态?

使用Retrofit2的API调用:我如何能够一直进行API调用,以更新数据而无需重新打开应用程序

泛型类型lambda函数参数作为函数参数

更改Jetpack Compose中选定的选项卡底线 colored颜色

使用Jetpack Compose创建特定于电视的布局

在Android中使用Room从SQlite数据库中获取实体列表的正确方式是什么?

从片段导航回来

如何将.txtassets资源 转换为ArrayList<;字符串>;kotlin格式?

在 Compose 中,当用户持续向下滚动时,LazyColumn 不会显示新项目

将输出写入已发布的 Android 应用程序中的日志(log)文件?

从 HiltViewModel @Injection 访问 Application()

java.lang.ExceptionInInitializerError -- 原因:java.lang.NullPointerException

如何在一个活动中预加载图像然后在另一个活动中加载它们?

未找到 com.android.tools.build:gradle:7.4.0 的匹配变体

如何在 compose 中使用 BottomSheetScaffold 为底页设置半展开高度?

EAS 构建失败,错误代码为 1,但 Expo Build 成功,我需要 EAS 构建来删除未使用的包,如 com.segment.analytics.android:analytics

在delphi中将Jnet_uri转换为Tbitmap