自定义AlertDialog边框

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>

你能提供CustomDialog的XML吗? - Michele
2个回答

5
我认为您无法使用AlertDialog.Builder去除边框。
您可以创建一个继承DialogCustomDialog类,在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);

也可以使用@android:color/transparent ;) - pablisco

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技巧。


是的,自API Level 11以来就可以实现。这取决于你的目标是什么。 - Ovidiu Latcu
不起作用。AlertDialog.Builder() 只接受一个 Context 参数。 - Khawar Raza
请查看更新。请注意需要使用“FrameLayout”技巧来插入您的视图。 - Peter Knego

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接