您的位置:首页 > Web前端 > CSS

改变support中AlertDialog的样式

2016-12-16 19:09 344 查看
android最近的support库提供了AlertDialog,可以让我们在低于5.0的系统使用到跟5.0系统一样的Material Design风格的对话框,但是使用了一段时间想到一些办法去改变对话框按钮字体的颜色,都不生效。

最近在网上找到了改变的方法,首先来说一下。




改变AlertDialog的样式

在values/styles.xml中定义一个主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="alertDialogTheme">@style/MyAlertDialogStyle</item>
....
</style>

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#FFC107</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#4CAF50</item>
</style>


在创建的对话框的时候,这样创建就可以了。

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
builder.setTitle("AppCompatDialog");
builder.setMessage("Lorem ipsum dolor...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: