开发者

Custom AlertDialog Borders

I am creating a custom dialog. Its example code is:开发者_运维知识库

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;
}

The problem is that when the dialog is shown, it has a gray background of the native dialog whose some top and bottom border is also shown with my custom dialog. Is there some way to show only my custom dialog view...???

The XML I am using is:

<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>


I don't think you can remove the borders by using AlertDialog.Builder.

What you can do is create a CustomDialog class that extends Dialog and in the constructor of your CustomDialog you inflate your customdialog.xml.

Also you will need to create a custom style for your dialog, that hides the borders. Here is an example:

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

Also define the transparent color:

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

And you will create your dialog using :

    CustomDialog dialog=new CustomDialog(this,R.style.CustomStyle);


Create a 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>

then use it:

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

Update

The constructor above is indeed API 11+. To work around this you need to extend AlertDialog (since its constructors are protected) and and then use constructor with theme parameter. To insert your custom view follow the instructions here - the FrameLayout trick described at the beginning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜