【问题标题】:Custom AlertDialog Borders自定义警报对话框边框
【发布时间】:2011-09-16 09:10:22
【问题描述】:

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

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?

标签: android customdialog


【解决方案1】:

我认为您不能使用 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 ;)
【解决方案2】:

创建custom theme

<?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),然后使用带有主题参数的构造函数。要插入您的自定义视图,请按照说明 here - 开头描述的 FrameLayout 技巧。

【讨论】:

  • 是的,这是可能的,因为 API 级别 11。取决于您的目标。
  • 不工作。 AlertDialog.Builder() 只接受单个 Context 参数。
  • 查看更新。请注意插入视图所需的 FrameLayout 技巧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
相关资源
最近更新 更多