【问题标题】:Android Dialog - Rounded Corners and TransparencyAndroid 对话框 - 圆角和透明度
【发布时间】:2013-05-27 12:18:09
【问题描述】:

我正在尝试制作一个带有圆角的自定义 android 对话框。我目前的尝试给了我这个结果。

如您所见,边角是圆角的,但白色边角仍然完好无损。

下面是我放在drawable文件夹中的xml,用于创建带有圆角的红色边框的蓝色对话框。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item> 
        <shape 
            android:shape="rectangle">
            <solid android:color="@color/transparent_black" />
            <corners android:radius="@dimen/border_radius"/>
        </shape>
    </item>   
    <item 
        android:left="@dimen/border_width" 
        android:right="@dimen/border_width"  
        android:top="@dimen/border_width"
        android:bottom="@dimen/border_width" >  

        <shape android:shape="rectangle"> 
            <solid android:color="@color/blue" />
            <corners android:radius="@dimen/border_radius"/>
        </shape>
    </item>    
</layer-list>

下面是对话框的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/fill"
android:orientation="vertical"
android:layout_margin="@dimen/spacing_normal"
android:padding="@dimen/spacing_normal"

android:background="@drawable/border_error_dialog" >

<RelativeLayout 
    style="@style/block"
    android:layout_gravity="center" >

    <ImageView
        android:id="@+id/imageView1"
        style="@style/wrap"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/content_description_filler"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        style="@style/error_text"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView1"
        android:text="@string/error_login" />

</RelativeLayout>

<Button
    android:id="@+id/button1"
    style="@style/wrap"
    android:layout_gravity="center"
    android:text="Button" />

</LinearLayout>

下面是我在其中创建对话框的 Activity。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {               
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

            View child = getLayoutInflater().inflate(R.layout.dialog_custom_tom, null);
            alertDialogBuilder.setView(child);

            AlertDialog alertDialog = alertDialogBuilder.create();

            alertDialog.show();
        }
    });
}

【问题讨论】:

标签: android android-layout dialog android-alertdialog transparent


【解决方案1】:

我找到的唯一解决方案是here。使用 Dialog 而不是 AlertDialog 并设置透明背景:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
因此,您不能使用构建器。但是,如果您遵循最佳准则,您也可以在 DialogFragment 的 onCreateDialog 回调中使用 new Dialog()。

这也适用于姜饼。

除了分层的drawable之外,还可以简化为一个形状,用xml元素作为边框。

【讨论】:

  • 更改为 Dialog 而不是 AlertDialog 终于为我解决了问题。谢谢我的朋友:)
  • 或者你可以使用 android:background="@android:color/transparent"
【解决方案2】:

在制作扩展 DialogFragment 的对话框并修复此问题时,我遇到了类似的问题:

dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0);

像这样:

public class ConfirmBDialog extends DialogFragment {

public static ConfirmBDialog newInstance() {
    ConfirmBDialog dialog = new ConfirmBDialog();
    Bundle bundle = new Bundle();
    dialog.setArguments(bundle);

    return dialog;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // This removes black background below corners.
    setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.confirm_dialog, container, true);
    getDialog().setCanceledOnTouchOutside(true);
    return view;
}

希望这会有所帮助。

【讨论】:

  • @greatergoodguy,如果这个答案能解决您的问题,请接受它
  • 经过多年的尝试,这是唯一对我有用的解决方法!谢谢!
  • 这很好用,谢谢!这是我发现的唯一适用于 DialogFragments 的解决方案...
  • 我将 setStyle() 移出 newInstance()。是的,它有帮助,但还有另一个问题。您无法应用主题,因此如果是这样,您的对话框可能会变窄,请参阅stackoverflow.com/questions/19133822/custom-dialog-too-small。请参阅下面的答案。
【解决方案3】:

试试看

myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

【讨论】:

    【解决方案4】:

    下面的代码解决了这个问题

    MyDialog mydialog = new MyDialog(this, "for testing",
                new myOnClickListener() {
    
                    @Override
                    public void onPositiveButtonClick() {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(),
                                "I am positive button in the dialog",
                                Toast.LENGTH_LONG).show();
                    }
    
                    @Override
                    public void onNegativeButtonClick() {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(),
                                "I am negative button in the dialog",
                                Toast.LENGTH_LONG).show();
                    }
                });
    
        // this will remove rectangle frame around the Dialog
        mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        mydialog.show();
    

    谢谢, 纳根德拉

    【讨论】:

      【解决方案5】:

      在您的 java 文件中保留以下代码并更改您的布局名称

      View mView =LayoutInflater.from(mContext).inflate(R.layout.layout_pob,null); 
      alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
      

      【讨论】:

        【解决方案6】:

        尝试使用这个,这对我有用

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

        【讨论】:

          【解决方案7】:

          在这些角落使用具有透明度的 9-patch PNG。

          【讨论】:

          【解决方案8】:
           public void initDialog() {
              exitDialog = new Dialog(this);
              exitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
              View view = View.inflate(this, R.layout.dialoglayout, null);
              exitDialog.setContentView(view);
              AdSize adSize = new AdSize(300, 250);
          
              dialogAdview = new AdView(this);
              dialogAdview.setAdUnitId(getResources().getString(R.string.banner_id));
              dialogAdview.setAdSize(adSize);
              RelativeLayout adLayout = (RelativeLayout) view.findViewById(R.id.adLayout);
              adLayout.addView(dialogAdview);
              AdRequest adRequest = new AdRequest.Builder()
                      .build();
              dialogAdview.loadAd(adRequest);
              dialogAdview.setAdListener(new AdListener() {
                  @Override
                  public void onAdLoaded() {
                      Log.d("Tag", "adLoaded");
                      super.onAdLoaded();
                  }
          
          
              });
          
              view.findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      exit = true;
                      onBackPressed();
                  }
              });
          
              view.findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      exit = false;
                      exitDialog.dismiss();
                  }
              });
          
          }
          

          dialoglayout.xml

           <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/custom_dialog_round"
              android:orientation="vertical">
          
          
              <TextView
                  android:id="@+id/text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_marginLeft="10dp"
                  android:text="Do you want to exit?"
                  android:textColor="#000"
                  android:textSize="18dp" />
          
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/text"
                  android:orientation="horizontal">
          
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:layout_weight="1"
                      android:gravity="center"
                      android:orientation="horizontal">
          
                      <TextView
                          android:id="@+id/yes_btn"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:background="@drawable/background_draw"
                          android:padding="8dp"
                          android:text="Yes"
                          android:textAlignment="center"
                          android:textColor="#9fa8da"
                          android:textSize="20dp" />
                  </LinearLayout>
          
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:layout_weight="1"
                      android:gravity="center"
                      android:orientation="horizontal">
          
                      <TextView
                          android:id="@+id/no_btn"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginRight="10dp"
                          android:background="@drawable/background_draw"
                          android:padding="8dp"
                          android:text="No"
                          android:textAlignment="center"
                          android:textColor="#d50000"
                          android:textSize="20dp" />
                  </LinearLayout>
          
          
              </LinearLayout>
          
          </LinearLayout>
          `
          

          custom_dialog_round.xml

          <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android">
              <solid
                  android:color="#fff"/>
              <corners
                  android:radius="10dp" />
              <padding
                  android:left="10dp"
                  android:top="10dp"
                  android:right="10dp"
                  android:bottom="10dp" />
          </shape>
          

          参考http://techamongus.blogspot.com/2018/02/android-create-round-corner-dialog.html

          【讨论】:

            【解决方案9】:
            • 更新

            我知道活动的背景是有道理的。因此,请使用@robert 对这些更改的回答。

            DialogFragment布局中设置宽度和高度或添加最小尺寸:

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="wrap_content" // Or match_parent, 300dp.
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:background="@drawable/white_round_corner_background"
                android:gravity="center"
                android:minWidth="300dp"
                android:minHeight="200dp"
                android:orientation="vertical"
                android:padding="15dp"
                >
            ...
            

            从所需活动的样式中删除&lt;item name="android:background"&gt;@color/...&lt;/item&gt;,并在活动的布局中设置这些背景。

            DialogFragment 写:

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                // This removes black background below corners.
                setStyle(DialogFragment.STYLE_NO_FRAME, 0);
            }
            
            • 旧变种

            根据robert的回答,你应该申请setStyle(STYLE_NO_FRAME, 0),但是出现了新的问题。如果你有一个狭窄的DialogFragment,比如Custom dialog too small,那么你应该遵循这个指南。

            添加到styles.xml这3行对话框大小:

            <style name="ErrorDialogTheme" parent="@android:style/Theme.Dialog">
                <item name="android:minWidth" type="dimen">300dp</item>
                <!-- This option makes dialog fullscreen and adds black background, so I commented it -->
                <!-- <item name="android:minHeight" type="dimen">200dp</item> -->
                <!-- This option doesn't work, so I commented it -->
                <!-- <item name="android:layout_width">match_parent</item> -->
            </style>
            

            DialogFragment 的布局中添加样式:

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                ...
                android:minWidth="300dp" // Optional, remove this line.
                android:minHeight="200dp" // Optional, remove this line.
                style="@style/ErrorDialogTheme"
                android:theme="@style/ErrorDialogTheme"
                >
            

            DialogFragment 的代码中写:

            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                // This removes black background. If not 0 as a parameter, black background will appear.
                setStyle(STYLE_NO_FRAME, 0)
            }
            
            // If you want a fullscreen dialog, use this, but it doesn't remove a black background.
            override fun onStart() {
                super.onStart()
                dialog.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT)
            }
            

            查看AndroidManifest.xml 并找到所有可以显示这些对话框的活动,检查android:theme="..." 主题并转到styles.xml。现在来看看这些主题的&lt;item name="android:background"&gt;@color/...&lt;/item&gt; 项目。应该有透明的颜色,否则这些项目可能不存在。如果你有这些背景项目,那么整个活动也会有这些背景和对话框!所以,如果你有一个上面有DialogFragment 的相机活动,你会看到这个。

            删除所需样式的背景项。也可能在代码中设置了背景,检查一下。

            Dialog with transparent background in Android 和许多页面中,它被写入添加其中之一:

            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            

            可能在onViewCreated()onCreateDialog() 中,但这对我没有帮助,因为Activity 的背景是在样式中设置的。

            在运行 Android 5.0.1 的三星 Galaxy S4 上测试。

            【讨论】:

            • @mandipgiri,也许,但在我的情况下,问题在于活动样式。我不得不更改几个属性。
            【解决方案10】:

            使用 CardView 并制作

            app:cardCornerRadius="dp" 根据形状xml。

            【讨论】:

              【解决方案11】:

              我将在此处发布我的解决方案,因为它可能会有所帮助。对我有用的解决方案是在布局 xml 以及启动对话框的 Activity 中设置可绘制资源,而无需从 AlertDialog 切换到 Dialog。

              这意味着在我们为对话框 alert_dialog_design.xml 创建设计的布局中,我们将使用我们自己定义的背景 alert_dialog_shape.xml 填充属性 android:background

              android:background="@drawable/alert_dialog_shape"
              

              但也在启动对话框的活动内部:

              alert.getWindow().setBackgroundDrawableResource(R.drawable.alert_dialog_shape);
              

              这样,您的自定义布局的父级(警报本身)将具有您想要的形状。使用这种方法,我实现了以下目标:

              https://i.stack.imgur.com/drCw3.png

              【讨论】:

                猜你喜欢
                • 2013-02-08
                • 2014-03-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-06-02
                • 2020-07-16
                相关资源
                最近更新 更多