【问题标题】:How to remove black border from AlertDialog builder如何从 AlertDialog 构建器中删除黑色边框
【发布时间】:2020-06-25 15:24:51
【问题描述】:

我使用AlertDialog.builder 创建了一个自定义对话框。在此对话框中,我没有显示标题。一切正常,但对话框中有黑色边框。那么谁能告诉我如何删除这个黑色边框?代码和截图如下。

java代码:

AlertDialog.Builder start_dialog = new AlertDialog.Builder(this);
            
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog2,
                                           (ViewGroup) findViewById(R.id.layout_root));
            
layout.setBackgroundResource(R.drawable.img_layover_welcome_bg);

Button btnPositiveError = (Button)layout.findViewById(R.id.btn_error_positive);   
btnPositiveError.setTypeface(m_facedesc);
            
start_dialog.setView(layout);
            
final AlertDialog alert = start_dialog.create();
alert.show();
            
btnPositiveError.setOnClickListener(new Button.OnClickListener()
{
    public void onClick(View v) 
    {
        alert.dismiss();
    }
});

ScrrenShot

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:
Without creating a custom background drawable and adding a special style just add

一行代码:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

更多关于对话框:

Dilaogs

【讨论】:

  • 它会清除所有背景,而不仅仅是边框!无论如何,它对我有用,我为 Layour 对话框使用了一种颜色。好!
  • 这个问题的最佳答案,不知道为什么人们建议为这个问题创建一个自定义主题。
【解决方案2】:

更改这行代码

AlertDialog.Builder start_dialog = new AlertDialog.Builder(this);

AlertDialog.Builder start_dialog = new AlertDialog.Builder(this).setInverseBackgroundForced(true);

一切就绪

【讨论】:

  • 它不会使其透明,只需将颜色更改为白色
【解决方案3】:

使用 Dialog 类而不是 AlertDialog

for eg - Dialog d = new Dialog(context, android.R.style.Theme_Translucent);

使用 setContentView 代替 setView。

并将其主题设置为透明。

【讨论】:

  • 我用过那个..但问题是它使对话框全屏..所以有什么选项可以防止它形成全屏
【解决方案4】:

只要你插入

Dialog start_dialog = new Dialog(this);

而不是这个

AlertDialog.Builder start_dialog = new AlertDialog.Builder(this);

【讨论】:

    【解决方案5】:

    我也有这个问题。最好使用Dialog 而不是AlertDialog。 这就是我的解决方案:

    Dialog dialog = new Dialog(getActivity(), R.style.Dialog_No_Border);
    dialog.setContentView(R.layout.some_dialog);
    

    some_dialog.xml 的内容

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="250dp"
            android:layout_height="350dp"
            android:orientation="vertical"
            android:background="@drawable/some_dialog_background">
    
            <TextView
               ...
           />
    
            <Button
               ...
           />
    
        </LinearLayout>
    

    styles.xml

    <style name="Dialog_No_Border" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowBackground">@color/transparent_color</item>
    </style>
    

    所以,我终于有了我的自定义对话框,背景没有边框。

    【讨论】:

      猜你喜欢
      • 2019-11-17
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2016-04-27
      • 2016-06-06
      相关资源
      最近更新 更多