android - 使用 setView(int layoutResId) 时如何从 AlertDialog 获取膨胀视图?
问题描述
我使用此代码创建自定义 AlertDialog:
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
问题是我无法获得膨胀的视图。dialog.findViewById(R.id.a_view_in_the_layout)
返回空值。
或者,我可以使用.setView(View.inflate(context, R.layout.layout, null)
,但这有时会使对话框填满屏幕并占用比setView(int layoutResId)
.
解决方案
而不是alert dialog
使用simple Dialog
它的简单且非常简单
final Dialog dialog = new Dialog(context);
dialog.setContentView((R.layout.layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView tvTitle = (TextView) dialog.findViewById(R.id.tvTitle);
tvTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
您不必为视图充气。
推荐阅读
- java - Dropwizard 解压请求过滤器
- python-3.x - 如何在 QTabWidget 上显示多光标?
- excel - EXCEL VBA:用户表单文本框中的科学记数法
- javascript - 我可以更改位于反应组件中的 div 的不透明度状态吗?
- javascript - 从 navigator.mediaDevices.getUserMedia() 获取字节流?
- amazon-web-services - AWS - 如何在不丢失实例和弹性 IP 的情况下暂停 Elastic Beanstalk 环境?
- python-3.x - 用于读取文件的机器学习 python 代码
- python-2.7 - 从命令行和其他脚本使用 argparse 参数
- objective-c - 如何在 Swift 中复制 CVPixelBuffer?
- amazon-web-services - AWS 使用 React Native 放大推送通知