【问题标题】:How to change the background of the custom alert dialog如何更改自定义警报对话框的背景
【发布时间】:2011-10-12 05:56:14
【问题描述】:

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

但我得到了这样的看法。

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

 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();

提前谢谢..!

【问题讨论】:

  • 发布您用于创建自定义警报对话框的 XML 布局。

标签: android android-layout


【解决方案1】:
Context mContext = getApplicationContext(); 
Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog); 
dialog.show();

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

【讨论】:

  • 嗨,谢谢您的有用回答,但我仍然遇到了一个问题。对话框即将全屏显示。如何避免这种情况。我已经更新了代码。
  • 这里,对话框的大小取决于列表视图的大小。我们可以尝试限制listview的高度。我还没有尝试过,但是这里有一个限制列表视图大小的想法:ViewGroup.LayoutParams params = customList.getLayoutParams();params.height = 300;customList.requestLayout(); 这里的高度在“px”中指定
  • 你好,可以加个截图吗
  • 我只能通过为对话框设置 xml 布局来完成工作。这是代码:Dialog d = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);d.setContentView(R.layout.list_layout);CustomDialogListAdapter customDialogAdapter;ListView customList=(ListView) d.findViewById(R.id.list_view);customDialogAdapter = new CustomDialogListAdapter(context,PaymentInfo.creditCardTypes, type);customList.setAdapter(customDialogAdapter);customList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);customList.setBackgroundColor(Color.WHITE);d.show();
  • 这里是list_layout.xml的代码:<RelativeLayout android:id="@+id/LinearLayout1" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"></ListView></RelativeLayout>
【解决方案2】:
 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();
   }

【讨论】:

  • 我的自定义布局中没有标题或按钮。你在这里定义的布局参数我在 xml 样式中做了同样的事情。输出仍然保持不变...... :(
【解决方案3】:

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

【讨论】:

  • 可能他指的是:dialog.getWindow().getDecorView().getRootView().setBackgroundColor(android.R.color.transparent);
【解决方案4】:

这将使它工作:

在列表视图中添加这个

android:cachecolorhint="#00000000"

【讨论】:

  • #00000000 是透明的值。
【解决方案5】:

在我的例子中,我通过使用这个代码 sn-p 得到了期望的输出:

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();

并在 list_main.xml 中,设置背景颜色为白色:

<?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);

【讨论】:

  • 我有自己的自定义视图要加载到列表中,因为我有自己的适配器。我该怎么办呢..?当您使用 ArrayAdapter 时。我用我的适配器尝试了你的方法,但它不起作用。
  • 我也仅将其用于自定义适配器...只需将 ArrayAdapter 替换为您自己的适配器..检查编辑后的答案..
猜你喜欢
  • 2011-03-08
  • 1970-01-01
  • 2017-06-02
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多