Custom AlertDialog Borders

时间秒杀一切 提交于 2020-01-15 01:20:31

问题


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>

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/7442450/custom-alertdialog-borders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!