3

我正在创建一个自定义对话框。它的示例代码是:

final AlertDialog dialog;

protected AlertDialog createDialog(int dialogId) {
    AlertDialog.Builder builder;
    builder = new AlertDialog.Builder(parent);
    AlertDialog fDialog = null;

    switch(dialogId) {
        case Constants.cusDialogtId:
            builder = new AlertDialog.Builder(parent);
            builder.setTitle("Title");
            LayoutInflater inflater = (LayoutInflater)parent.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.customdialog, null);
            builder.setView(view);
            fDialog = builder.create();
            break;
    }
    dialog = fDialog;
    return dialog;
}

问题是当显示对话框时,它具有本机对话框的灰色背景,其一些顶部和底部边框也显示在我的自定义对话框中。有没有办法只显示我的自定义对话框视图......???

我使用的 XML 是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="@drawable/bgsmall" >
<EditText android:id="@+id/redeemamount"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:layout_marginTop="10dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:hint="Enter amount"
android:inputType="numberDecimal">
</EditText>             
<Button android:id="@+id/submitRedeemAmountButton"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:text="Submit"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:background="@drawable/buttoncorner"
android:layout_marginTop="20dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginBottom="20dip">
</Button>
</LinearLayout>
4

2 回答 2

5

我认为您不能使用AlertDialog.Builder.

你可以做的是创建一个CustomDialog扩展的类,Dialog并在你的构造函数中CustomDialog膨胀你的customdialog.xml.

此外,您还需要style为您的对话框创建一个自定义,以隐藏边框。这是一个例子:

    <style
      name="CustomStyle"
      parent="android:Theme.Dialog">
      <item
          name="android:windowBackground">@color/transparent</item>
      <item
          name="android:windowContentOverlay">@null</item>
    </style>

还要定义透明色:

   <color
      name="transparent">#00000000</color>

您将使用以下命令创建对话框:

    CustomDialog dialog=new CustomDialog(this,R.style.CustomStyle);
于 2011-09-16T09:23:03.877 回答
2

创建自定义主题

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@null</item>
    </style> 
</resources>

然后使用它:

builder = new AlertDialog.Builder(parent, R.style.CustomDialog);

更新

上面的构造函数确实是 API 11+。要解决此问题,您需要扩展 AlertDialog (因为它的构造函数是protected),然后使用带有主题参数的构造函数。要插入您的自定义视图,请按照此处的说明进行操作-FrameLayout开头描述的技巧。

于 2011-09-16T09:22:53.600 回答