如何更改Android对话框或抽屉菜单中的黑色

时间:2017-07-27 21:23:02

标签: android android-layout android-alertdialog

在Android中,当我们显示警告对话框,进度对话框,打开抽屉等等时,似乎我们在屏幕上有一个透明的黑色图层,然后是一种对话框,如下图:

我的目标是更改透明的黑色图层,而不是对话框的背景,请看这张图片:

enter image description here

如何将此透明黑色图层的颜色更改为红色或任何颜色?例如,在XML中或通过java中的代码

这是MyExitDialog类的代码

public class MyExitDialog extends Dialog implements android.view.View.OnClickListener
{
    DatabaseHandler userDB;

    public Activity c;
    public Dialog d;
    public Button yes, no;

    public MyExitDialog(Activity a)
    {
        super(a);
        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mydialog);
        yes = (Button) findViewById(R.id.id_exit_yes);
        no = (Button) findViewById(R.id.id_exit_no);
        yes.setOnClickListener(this);
        no.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId()) {
            case R.id.id_exit_yes:
                //c.finish();
                DrawerActivity.userDB=new DatabaseHandler(MyExitDialog.this.getContext());
                DrawerActivity.userDB.open();
                DrawerActivity.userDB.logOut();
                DrawerActivity.userDB.close();
                DrawerActivity.changeDrawerItems("logout");
                break;
            case R.id.id_exit_no:
                dismiss();
                break;
            default:
                break;
        }
        dismiss();

    }
}

这就是我使用它的方式:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    int id = item.getItemId();
    Fragment myFragment = null;

    View myView;
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BYekan.ttf");

    if (id == R.id.nav_home) {

    } else if (id == R.id.nav_register) {


    } else if (id == R.id.nav_change) {

    } else if (id == R.id.nav_login) {

    } else if (id == R.id.nav_factors) {

    } else if (id == R.id.nav_logout) {
        MyExitDialog myDialog = new MyExitDialog(DrawerActivity.this);
        myDialog.show();
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

2 个答案:

答案 0 :(得分:1)

对于AlertDialog,您可以将自定义主题设置为第二个参数。

AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.CustomDialog);

在style.xml中设置customDialog样式

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

最后,您可以在colors.xml中设置自定义颜色

<resources>
    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>
    <item name="red" type="color">#FFFF4444</item>
    <item name="darkblue" type="color">#FF0099CC</item>
    <item name="darkpurple" type="color">#FF9933CC</item>
    <item name="darkgreen" type="color">#FF669900</item>
    <item name="darkorange" type="color">#FFFF8800</item>
    <item name="darkred" type="color">#FFCC0000</item>
</resources>

有很多颜色可供选择。

<强>更新

尝试从

进行更改
public MyExitDialog(Activity a)
{
    super(a);
    this.c = a;
}

到此

public MyExitDialog(Activity a)
{
    super(a, R.style.CustomDialog);
    this.c = a;
}

清洁代码:

使用它作为构造函数。

public MyExitDialog(Activity a, int resID)
{
    super(a, resID);
    this.c = a;
}

并在参数中添加resID,如下所示。

MyExitDialog myDialog = new MyExitDialog(DrawerActivity.this, R.style.CustomDialog);
myDialog.show();

<强>更新

我再次抱歉提供了错误的信息。请注意,我没有使用android:background。将其更改为android:windowBackground

<resources>
    <style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowBackground">@color/red</item>
    </style>
</resources>

希望它有所帮助。

答案 1 :(得分:1)

更改抽屉覆盖背景:

yourDrawerLayout.setScrimColor(Color.RED)