繁体   English   中英

尝试更改自定义对话框背景颜色

[英]Trying to change custom dialog Background color

好的,所以我正在尝试将对话框的背景从白色更改为深蓝色。 但是,当我长按其中一个网格元素时,对话框如下所示:

它看起来如何

我试图让它看起来像这样(这是一个photoshop):

我希望它看起来如何的 PhotoShop

这是我用于编辑对话框的 XML 代码片段

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="16dp"
android:background="@color/customBG">

自定义对话框的 Java 代码

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.edit_game_dialog,null);

    editTitle = view.findViewById(R.id.editTitle);
    editTitle.setText(currentTitle);
    imageView = view.findViewById(R.id.item_image_dialog);
    imageView.setImageResource(currentImage);
    changeImageBt = view.findViewById(R.id.change_image);

    changeImageBt.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {

        }
    });

    builder.setView(view).setTitle("Edit game")
            .setPositiveButton("Apply Changes", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                {
                    String title = editTitle.getText().toString();
                    int image = R.drawable.blank; //PLACE HOLDER CODE
                    editGameDialogListener.applyChanges(pos,title,image);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                {

                }
            });

    return builder.create();
}

我认为你应该使用 Dialog 而不是 AlertDialog。 警报对话框有自己的标题和按钮。

使用对话框,您将受益于定义标题和按钮。

根据您的设计需要创建一个布局并在对话框中进行设置。

class ABC(context: Context) : Dialog(context) {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.your_custom_layout)
}
}

创建对话框时,可以将主题作为第二个参数传递

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialogTheme);

并设置自定义主题以覆盖您需要的任何内容。 对于背景颜色,这样的东西应该可以工作:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">@color/customBG</item>
</style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM