how to set button color in dialog box

谁说我不能喝 提交于 2020-01-07 03:15:51

问题


i have created a dialog box which has "accept" and "reject" buttons, but the color of the button is in default color. can anyone provide the code to set the color of the buttons in green and red.


回答1:


By creating a custom drawable.. for example..

button.setBackgroundResource(R.drawable.custom_button);

custom_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item> 
  <shape android:shape="rectangle">
    <corners android:radius="8dip" />  
        <stroke android:width="1dp" android:color="your color" /> 
        <solid android:color="@color/ceruleo" /> 
    </shape> 
 </item> 

 <item android:top="1dp" android:bottom="1dp" android:left="1dp" android:right="1dp">  
  <shape android:shape="rectangle"> 
    <corners android:radius="8dip" /> 
        <stroke android:width="1dp" android:color="your color" /> 
        <solid android:color="@color/ceruleo" /> 
    </shape> 
 </item> 

 </layer-list> 



回答2:


Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null)
    b.setBackgroundColor(Color.BLUE);

This Code will help you. You can set color to Negative or positive button as you wish.




回答3:


ANother way is , you can create dialogue activity for custom dialogues. just create activity and in manifest

android:theme="@android:style/Theme.Dialog

create layout for this. by this way you can create any design you want



来源:https://stackoverflow.com/questions/13701162/how-to-set-button-color-in-dialog-box

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