Android更改对话框线的颜色

时间:2014-03-21 02:49:40

标签: android android-alertdialog

如何更改以下Dialog中的线条颜色:

Dialog title divider

3 个答案:

答案 0 :(得分:0)

不幸的是,你必须给自己的布局充气。这是你的答案:

How can I change the color of AlertDialog title and the color of the line under it

答案 1 :(得分:0)

创建自定义AlertDialog并使用setCustomTitle设置自定义标题视图。

 public AlertDialog.Builder setCustomTitle (View customTitleView)

使用自定义视图customTitleView设置标题。对于大多数标题,方法setTitle(int)和setIcon(int)应该足够了,但如果标题需要更多自定义,则会提供此方法。使用它将通过其他方法替换标题和图标集。

来自此链接.. http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCustomTitle%28android.view.View%29

答案 2 :(得分:0)

如果您不想创建自定义布局,则可以在继承View然后调用Resources.getIdentifier后使用AlertDialog访问分隔符AlertDialog.findViewById。这是一个简单的例子:

/**
 * {@inheritDoc}
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final CustomDialog cd = new CustomDialog(this);
    cd.setTitle("Title");
    cd.setMessage("Message");
    cd.show();
}

private static final class CustomDialog extends AlertDialog {

    private CustomDialog(Context context) {
        super(context);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Resources res = getContext().getResources();
        final int id = res.getIdentifier("titleDivider", "id", "android");
        final View titleDivider = findViewById(id);
        if (titleDivider != null) {
            titleDivider.setBackgroundColor(Color.RED);
        }
    }
}

Example