Android 如何更改自定义警报对话框的背景

Android 如何更改自定义警报对话框的背景,android,android-layout,Android,Android Layout,我有一个自定义警报对话框,其中我设置了一个列表视图,其背景为白色 但我看到的是这样的景象 这是适合整个屏幕的屏幕截图 Dialog d = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar); CustomDialogListAdapter customDialogAdapter; ListView customList = new ListView(context);

我有一个自定义警报对话框,其中我设置了一个列表视图,其背景为白色

但我看到的是这样的景象

这是适合整个屏幕的屏幕截图

 Dialog d = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);       
        CustomDialogListAdapter customDialogAdapter;
        ListView customList = new ListView(context);
        customList.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        customList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        customDialogAdapter = new CustomDialogListAdapter(context,PaymentInfo.creditCardTypes, type);
        customList.setAdapter(customDialogAdapter);
        customList.setBackgroundColor(Color.WHITE);
        d.setContentView(customList);
        d.show();

提前谢谢

获取父视图布局并设置其背景:)

这将使其正常工作:

在Listview中添加以下内容


android:cachecolorhint=“#00000000”

在我的例子中,我通过使用以下代码片段获得了所需的输出:

Dialog dialog2 = new Dialog(Activity.this);
ListView modeList = new ListView(Activity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);


 String[] stringArray = new String[] { "No results" };

  ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(Activity.this, R.layout.list_main, R.id.item_subtitle, stringArray);
  modeList.setAdapter(modeAdapter);

 builder.setView(modeList);
 dialog2 = builder.create();
 dialog2.show();

上面的代码将生成一个透明的对话框。因此,您的ListView背景将是对话框的唯一背景。

发布您用于创建自定义警报对话框的XML布局。#00000000是透明的值。我有自己的自定义视图要加载到列表中,因为我有自己的适配器。我该怎么办。。?当您使用ArrayAdapter时。我用我的适配器尝试了你的方法,但它不起作用。我也只使用自定义适配器…用你自己的适配器替换ArrayAdapter..检查编辑的答案..我的自定义布局中没有标题或按钮。您在这里定义的布局参数也是用xml样式定义的。输出仍然保持不变…:(嗨,谢谢你有用的回答,不过我还是遇到了一个问题。对话框将全屏显示。如何避免。我已经更新了代码。在这里,对话框的大小取决于listview的大小。我们可以尝试限制listview的高度。我还没有尝试过,但这里有一个限制listview大小的主意:
ViewGroup.LayoutParams params=customList.getLayoutParams();
params.height=300;
customList.requestLayout();
这里高度是在“px”中指定的嗨,你能添加一个屏幕快照吗?我只能通过使用对话框的xml布局来获得dis。下面是代码:
dialog d=new dialog(context,android.R.style.Theme_transparent_NoTitleBar);
d.setContentView(R.layout.list_layout);
CustomDialogListAdapter\customDialogAdapter;
ListView自定义列表=(ListView)d.findViewById(R.list\u视图);
customDialogAdapter=新建CustomDialogListAdapter(context,PaymentInfo.creditCardTypes,type,type)
customList.setAdapter(customDialogAdapter);
customList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
customList.setBackgroundColor(Color.WHITE);
d.show()
这是list_layout.xml的代码:
可能他指的是:
dialog.getWindow().getDecorView().getRootView().setBackgroundColor(android.R.color.transparent);
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="7dp"
android:background="#ffffff">
</LinearLayout>
    builder.setTitle(" title");
            MySimpleAdapter adapter = new MySimpleAdapter(Activity.this, data , R.layout.list_main, 
                    new String[] { "name", "distance" ,"phone","web"}, 
                    new int[] { R.id.item_title, R.id.item_subtitle ,R.id.item_subtitle1 ,R.id.item_subtitle2});
            modeList.setAdapter(adapter);
 AlertDialog.Builder screenDialog = new AlertDialog.Builder(AndroidCaptureScreen.this);
    screenDialog.setTitle("Captured Screen");



    TextView TextOut = new TextView(AndroidCaptureScreen.this);
    TextOut.setText(EditTextIn.getText().toString());
    LayoutParams textOutLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    TextOut.setLayoutParams(textOutLayoutParams);

    ImageView bmImage = new ImageView(AndroidCaptureScreen.this);
    bmImage.setImageBitmap(bmScreen);
    LayoutParams bmImageLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    bmImage.setLayoutParams(bmImageLayoutParams);

    LinearLayout dialogLayout = new LinearLayout(AndroidCaptureScreen.this);
    dialogLayout.setOrientation(LinearLayout.VERTICAL);
    dialogLayout.addView(TextOut);
    dialogLayout.addView(bmImage);
    screenDialog.setView(dialogLayout);

    screenDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {

         }
        });
    screenDialog.show();
   }
Context mContext = getApplicationContext(); 
Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog); 
dialog.show();