在棒棒堂DialogFragment按钮的颜色变化 [英] DialogFragment buttons color change in Lollipop

查看:624
本文介绍了在棒棒堂DialogFragment按钮的颜色变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的片段看的正/负键与我申请了,所以我想改变颜色不仅标题的应用程序和调色板的其余部分保持一致,同时也:

I would like my Fragments to look consistent with the rest of the app and color palette which I applied so I would like to change the colors not only of title, but also of positive/negative buttons:

我试图做到这一点是这样,但unfortunetaly这是行不通的:

I tried to do this like this, but unfortunetaly it doesn't work:

public void onStart() {
        super.onStart();
        Dialog d = getDialog();
        int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
        View divider = d.findViewById(dividerId);

        if(currentapiVersion< Build.VERSION_CODES.LOLLIPOP) {
            divider.setBackgroundColor(getResources().getColor(R.color.accent));
            LinearLayout ll = (LinearLayout) d.findViewById(R.id.dialog_background);
            ll.setBackgroundResource(R.drawable.backrepeat_reversed);
        }
        if(currentapiVersion == Build.VERSION_CODES.LOLLIPOP) {
            int buttonsId = d.getContext().getResources().getIdentifier("android:id/negativeButton", null, null);
            Button b = (Button) d.findViewById(buttonsId);
            b.setTextColor(getResources().getColor(R.color.accent));
        }
        int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
        TextView tv = (TextView) d.findViewById(textViewId);

        tv.setTextColor(getResources().getColor(R.color.accent));
    }

如何更改这些按钮的颜色?也许这是不可能性,通过styles.xml文件,做到在整个应用程序?

How to change the color of these buttons? Maybe it is possibile to do it in the whole application through styles.xml file?

推荐答案

如果您可以使用创建对话框 AlertDialog 对我下面的工作:

If you can create the dialog using AlertDialog the following worked for me:

public class DialogTest extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity()).setTitle("Test")
                .setMessage("This is a dialog.")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                }).show();
    }

    @Override
    public void onStart() {
        super.onStart();
        ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.RED);
        ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
    }
}

这篇关于在棒棒堂DialogFragment按钮的颜色变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆