Is it possible to add drawables to the positive, negative and neutral buttons of an AlertDialog? If yes, then how?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You can't add buttons in the onCreateDialog and MUST do it in the onPrepareDialog because AlertDialog are handled in a very special way by android :
you actually don't really hold a reference to the real dialog when you use alert dialog, the object you get by using AlertDialog.Builder.create() is just a face to an inner controller.
And before create is actually called, there is no such controller in the jvm. Just the facade. So, until this method is called (at the end of onCreateDialog if you let your activity manage its own dialogs), the real controller doesn't exist, and real buttons don't neither.
brand new SOF commenter, Stéphane
As @aaronvargas said, use
onShowListener
. I will improve his answer a little bit, since for older/smaller devices the image overlaps the text. Here is theonShow
code:Here is an utility function to center a left image and text inside a
Button
:This last function uses the width of the
Button
to make the calculation, so you must check that you are calling this in the right place. That is, the width should be other than zero. In this case, calling it fromonShow
is the right place :).Since
onPrepareDialog
is deprecated, you can use theonShowListener
instead.Also you should set the Drawable bounds or it will be placed far left.
Output of Code below
This can be done by getting a reference to the button using the getButton() method:
Note that you must use the getButton() AFTER calling the show() method, otherwise you'll get a NullPointerException..
After you have built the
AlertDialog
inonCreateDialog
you can use the following code inonPrepareDialog
to add an image to the positive button:Trying to add the drawable to the button in the
onCreateDialog
method does not seem to work.1.first create a new layout file to store the imagebuttons:new_layout.xml;
2.add the code below to where you want the dialogue to show:
link:http://blog.csdn.net/willproud/article/details/9191971