设置自定义警报对话框的标题和(标题)图标
我无法为自定义警报对话框设置标题或(标题)图标。 我的代码:
public class AddingFavoriteDialog extends AlertDialog {
private OnAddingFavoriteListener onAddingFavoriteListener;
private Context context;
private GeocodingManager geocodingManager;
private FavoritesActivity favoritesActivity;
public AddingFavoriteDialog(Context context, OnAddingFavoriteListener onAddingFavoriteListener) {
super(context, android.R.style.Theme_Dialog);
this.context = context;
this.onAddingFavoriteListener = onAddingFavoriteListener;
this.geocodingManager = new GeocodingManager(context);
this.favoritesActivity = (FavoritesActivity) context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adding_favorite_dialog2);
setTitle("MYTITLE");
setIcon(R.drawable.star_gold);
}
我做错了什么?我还尝试通过在 onCreate()
以及构造函数中调用 super.setTitle("MYTITLE");
来设置它。
编辑:甚至setButton(BUTTON_POSITIVE, context.getString(R.string.button_value_OK), new OnClickListener() {...}
似乎不起作用。
I don't manage to set a neither a title nor a (title) icon to my custom alert dialog.
My code:
public class AddingFavoriteDialog extends AlertDialog {
private OnAddingFavoriteListener onAddingFavoriteListener;
private Context context;
private GeocodingManager geocodingManager;
private FavoritesActivity favoritesActivity;
public AddingFavoriteDialog(Context context, OnAddingFavoriteListener onAddingFavoriteListener) {
super(context, android.R.style.Theme_Dialog);
this.context = context;
this.onAddingFavoriteListener = onAddingFavoriteListener;
this.geocodingManager = new GeocodingManager(context);
this.favoritesActivity = (FavoritesActivity) context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adding_favorite_dialog2);
setTitle("MYTITLE");
setIcon(R.drawable.star_gold);
}
What am i doing wrong? I also tried to set it by calling super.setTitle("MYTITLE");
in onCreate()
as well as in the constructor.
EDIT: Even setButton(BUTTON_POSITIVE, context.getString(R.string.button_value_OK),
seems not to work.
new OnClickListener() {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
setView
而不是setContentView
,因为setContentView
替换了AlertDialog
中的所有内容,包括默认标题栏和图标(以及按钮等) 。相反,setView
仅替换中间部分(消息,如果您愿意的话)。如果需要,请使用
LayoutInflater
。Use
setView
instead ofsetContentView
, becausesetContentView
replaces everything in theAlertDialog
, including the default title bar and icon (and buttons etc.). Instead,setView
only replaces the middle part (the message, if you will).Use
LayoutInflater
if you need to.扩展 Dialog 而不是 AlertDialog 将解决该问题。
Extending Dialog instead of AlertDialog will fix the problem.