如何去掉自定义对话框的矩形边框

5
我自定义了一个对话框:
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
    super(context);
    requestWindowFeature(Window.FEATURE_NO_TITLE);      
    setContentView(R.layout.custom_diolog_main);
    tv=(TextView) findViewById(R.id.content);
    tv.setText(Stringcontent);
    close = (Button) findViewById(R.id.close);
    close.setOnClickListener(this);
}

@Override
public void onClick(View v) {       
    if (v == close)
        dismiss();
}

 }

这个 XML 是

   <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout  
   xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="100dip" 
android:orientation="vertical"
android:background="@drawable/custom_diolog_bg"
android:layout_width="250dip">
<TextView android:layout_height="wrap_content"
    android:textColor="#000" 
    android:textStyle="bold" 
    android:textSize="18sp"
    android:id="@+id/content"
    android:layout_marginLeft="15dip"
    android:layout_marginTop="5dip"
    android:layout_alignParentTop="true"
    android:layout_width="250dip" 
    android:text=" Custom Dialog "/>


<Button android:layout_width="70dip"  
    android:layout_marginLeft="80dip"
    android:background="@drawable/custom_dialog_button_bg"
    android:layout_alignParentBottom="true"
    android:layout_height="40dip" android:text="关闭"   
        android:id="@+id/close"></Button>
 </RelativeLayout>

我的对话框非常好,但是custom_dialog_bg是一个圆角矩形图像,当我显示我的对话框时,它会显示一个系统框架在我的自定义对话框后面,所以我使用了this.getwindow.setBackgroundDrawable(null),然后系统框架似乎已经被移除了,但是只有四个角没有被移除,我们还可以看到暗色的四个角,因为我使用了圆角矩形图像。所以我的问题是如何移除所有的框架,使我的对话框看起来非常好。
图片链接: http://i.stack.imgur.com/EG7oz.jpg,所以你可以看到最后有一个暗色的边框,如何去掉它?谢谢。
5个回答

20

对我有效的解决方案

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

Dialog dialog = new Dialog(this, R.style.DialogTheme);

6

在调用setContentView()之前,请使用以下行:

getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));

会完美地工作。

2

对话框 mydialog = new dialog (this,android.R.style.Theme_Translucent_NoTitleBar);


该代码是在Android中创建一个透明无标题栏的对话框。

1
如果我使用这个,对话框会显示在屏幕左上角而不是居中。 - pengwang

1

而不是调用

super(context);

调用

super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

更新:请使用此 XML 布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="100dip"
    android:orientation="vertical"
    android:background="@drawable/custom_diolog_bg"
    android:layout_width="250dip">
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">
        <TextView
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textStyle="bold"
        android:textSize="18sp"
        android:id="@+id/content"
        android:layout_marginLeft="15dip"
        android:layout_marginTop="5dip"
        android:layout_alignParentTop="true"
        android:layout_width="250dip"
        android:text=" Custom Dialog " />
    <Button
        android:layout_width="70dip"
        android:layout_marginLeft="80dip"
        android:background="@drawable/custom_dialog_button_bg"
        android:layout_alignParentBottom="true"
        android:layout_height="40dip"
        android:text="关闭"
        android:id="@+id/close"></Button>
    </LinearLayout>
</RelativeLayout>

如果我使用这个,对话框会显示在屏幕左上角而不是居中。 - pengwang

1

尝试这个,对我来说非常有效。

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
final LayoutInflater inflater = (LayoutInflater) wrapper.getSystemService(LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);

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