修改AlertDialog的宽高大小,Button的字体颜色等

来源:互联网 发布:mac dock栏 图标合并 编辑:程序博客网 时间:2024/05/29 18:56

    <style name="AlertDialog" parent="@style/Theme.AppCompat.Light.Dialog">        <!-- 这里设置背景为透明,为了隐藏边框 -->        <item name="android:windowBackground">@color/white</item>        <item name="android:windowNoTitle">true</item>        <!-- 这里是修改顶部标题背景颜色,具体颜色自己定,可以是图片 -->        <item name="android:topDark">@color/white</item>        <!-- 这里是修改内容区域背景颜色 -->        <item name="android:centerDark">@color/white</item>        <item name="android:windowIsTranslucent">true</item>        <item name="buttonBarPositiveButtonStyle">@style/positive</item>        <item name="buttonBarNegativeButtonStyle">@style/negative</item>    </style>    <style name="positive">        <item name="android:textColor">@color/colorAccent</item>    </style>    <style name="negative">        <item name="android:textColor">@color/colorAccent</item>    </style>
可以看到这里使用了style中的属性来修改Button的字体颜色:

buttonBarPositiveButtonStyle
buttonBarNegativeButtonStyle。



这里设置大小请注意:show()需要在setLayout()之前。

            final AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this, R.style.AlertDialog);            DisplayMetrics metrics = new DisplayMetrics();            getWindowManager().getDefaultDisplay().getMetrics(metrics);            double width = metrics.widthPixels * 0.9;            double height = metrics.heightPixels * 0.4;            AlertDialog alertDialog = builder.setMessage(message)                    .setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int which) {                            result.confirm();                        }                    })                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int which) {                            result.cancel();                        }                    })                    .create();            alertDialog.show();            alertDialog.getWindow().setLayout((int)width, ViewGroup.LayoutParams.WRAP_CONTENT);





参考:

http://blog.csdn.net/gs12software/article/details/48224885

https://stackoverflow.com/questions/4406804/how-to-control-the-width-and-height-of-the-default-alert-dialog-in-android





原创粉丝点击