Set title and (title) icon for a custom alert dialog

纵然是瞬间 提交于 2019-12-13 05:35:10

问题


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), new OnClickListener() {...} seems not to work.


回答1:


Use setView instead of setContentView, because setContentView replaces everything in the AlertDialog, 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.




回答2:


Extending Dialog instead of AlertDialog will fix the problem.



来源:https://stackoverflow.com/questions/4712331/set-title-and-title-icon-for-a-custom-alert-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!