当前位置: 首页 > 知识库问答 >
问题:

带圆角的Android AlertDialog

吉玉石
2023-03-14

我一直在努力使我的警告对话框圆角,但不知何故,我不能。我试过了,但失败了。我试着关注这个博客http://blog.stylingandroid.com/archives/271并以此为基础制作了我的风格。

顺便说一句,现在补充我的问题。我的一些新发现。上面链接中的代码在2.3.3(GB)上运行良好,但在ICS中根本不起作用。一些改变使代码中断。

我想避免创建9个补丁图像,因此我使用形状。9补丁图像是我最后要尝试的事情。我知道Android警报对话框样式是使用9补丁图像。在抛出这个问题之前,我已经查过了。

/资源/价值/主题。xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="@android:style/Theme.Dialog">
        <item name="android:alertDialogStyle">@style/dialog</item>
    </style>


</resources>

/res/值/样式。xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme" parent="android:Theme.Light" />

    <style name="myImageView">

        <!-- 3dp so the background border to be visible -->
        <item name="android:padding">3dp</item>
        <item name="android:background">@drawable/image_drawable</item>
        <item name="android:scaleType">fitCenter</item>
    </style>

    <style name="dialog">
        <item name="android:fullDark">@drawable/dialog_body</item>
        <item name="android:topDark">@drawable/dialog_title</item>
        <item name="android:centerDark">@drawable/dialog_body</item>
        <item name="android:bottomDark">@drawable/dialog_footer</item>
        <item name="android:fullBright">@drawable/dialog_body</item>
        <item name="android:centerBright">@drawable/dialog_body</item>
        <item name="android:topBright">@drawable/dialog_title</item>
        <item name="android:bottomBright">@drawable/dialog_footer</item>
        <item name="android:bottomMedium">@drawable/dialog_footer</item>
        <item name="android:centerMedium">@drawable/dialog_body</item>
    </style>

</resources>

/res/drawable/dialog_title.xml

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="-1dp">
    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF" />
        <corners android:topLeftRadius="5dp" android:topRightRadius="5dp" />
        <stroke android:color="#FFFFFF" android:width="1dp" />
    </shape>
</inset>

/res/drawable/dialog\u body。xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#FFFFFFFF" android:endColor="#FFFFFFFF"
        android:angle="270" />
</shape>

/res/drawable/dialog\u页脚。xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#FFFFFF" />

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp" />

    <stroke
        android:width="1dp"
        android:color="#FFFFFF" />

</shape>

res/layout/dialog\u布局。xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="90dp"
        android:layout_toLeftOf="@+id/textView1"
        android:background="@drawable/button_selector"
        android:text="Ok"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/button1"
        android:layout_marginRight="48dp"
        android:background="@drawable/button_selector"
        android:text="More"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="41dp"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

AlertDialog的我的代码:

public static void createYesNoDialog(final Context context, String positivebuttonname,
            String negativebuttonname, String message, int messagedrawable, String headermessage,
            final DialogResponse dr) {
        final DialogResponse dialogResponse = dr;
        ContextThemeWrapper ctw = new ContextThemeWrapper(context,
                com.gp4ever.worldlogo.quiz.R.style.MyTheme);

        AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
        LayoutInflater inflater = (LayoutInflater)context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(com.gp4ever.worldlogo.quiz.R.layout.dialog_layout, null);
        TextView text = (TextView)layout.findViewById(com.gp4ever.worldlogo.quiz.R.id.textView1);
        Button buttonOk = (Button)layout.findViewById(com.gp4ever.worldlogo.quiz.R.id.button1);
        Button buttonMore = (Button)layout.findViewById(com.gp4ever.worldlogo.quiz.R.id.button2);
        text.setText(message);
        if (messagedrawable > 0) {
            text.setCompoundDrawablesWithIntrinsicBounds(messagedrawable, 0, 0, 0);
        } else if (messagedrawable == 0)
            text.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        builder.setView(layout);
        builder.setCancelable(false);
        builder.setTitle(headermessage);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        final AlertDialog dialog = builder.create();

        buttonOk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });
        buttonMore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });

}

我的当前输出:

我没有任何圆角。我可以看出它与通常的款式不同。即使我在绘图板上更改了半径,角点也不会反映这些更改。

共有3个答案

张华池
2023-03-14

只需使用官方材质组件库中包含的MaterialAlertDialogBuilder

new MaterialAlertDialogBuilder(MainActivity.this,R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
            .setTitle("Dialog")
            .setMessage("Lorem ipsum dolor ....")
            .setPositiveButton("Ok", /* listener = */ null)
            .setNegativeButton("Cancel", /* listener = */ null)
            .show();

然后使用shapeEmeraranceOverlay属性定义样式。

 <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
  </style>

  <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
  </style>
朱兴运
2023-03-14

距离@iDroid Explorer答案仅有一步

在生成对话框时添加此行

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

这将使矩形消失(实际上是透明的),并得到一个完美的圆形对话框。

江鹏
2023-03-14

您可以使用以下代码执行此操作:

自定义对话框。爪哇:

public class MainActivity extends Activity{

    private static final int ALERT_DIALOG = 1;

    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );

        ( (Button) findViewById( R.id.button1 ) )
            .setOnClickListener( new OnClickListener()
                {
                    public void onClick( View v )
                    {
                        showDialog( ALERT_DIALOG );
                    }
                }
            );
    }

    @Override
    protected Dialog onCreateDialog( int id ){
        Dialog dialog = null;
        if ( id == ALERT_DIALOG )
        {
            ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.MyTheme );
            AlertDialog.Builder builder = new AlertDialog.Builder( ctw );
            builder.setMessage( "Hello World" )
                .setTitle( "Alert Dialog" )
                .setIcon( android.R.drawable.ic_dialog_alert )
                .setCancelable( false )
                .setPositiveButton( "Close", new DialogInterface.OnClickListener()
                    {
                        public void onClick( DialogInterface dialog, int which )
                           {
                                dialog.dismiss();
                           }
                        } 
                    );
            dialog = builder.create();
        }
        if ( dialog == null )
        {
            dialog = super.onCreateDialog( id );
        }
        return dialog;
     }
 }

对话标题。xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetBottom="-1dp">
    <shape android:shape="rectangle">
        <solid android:color="#000000" />
        <corners android:topLeftRadius="20dp" android:topRightRadius="20dp" />
        <stroke android:color="#7F7F7F" android:width="1dp" />
    </shape>
</inset>

dialog_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#7F7F7F" />
    <corners android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp" />
    <stroke android:color="#7F7F7F" android:width="1dp" />
</shape>

只需在以下位置更改半径量:

对话框标题。xml

dialog_footer.xml

这将产生以下输出:

希望这对你有帮助。

更新:
我不是专家,但这就是我发现的。它可能是对的,也可能是错的。经过多次尝试,我最终得出以下结论:

1-ContextThemeWrapper不适用于API 14,它适用于姜饼和旧版本,但适用于API

2-克服上述问题,使其在API上工作

ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.MyTheme );
AlertDialog.Builder builder= new AlertDialog.Builder( ctw );

为此:

AlertDialog.Builder builder= new AlertDialog.Builder( this,R.style.MyTheme );

但你需要改变:

android:minSdkVersion="8"  

android:minSdkVersion="11" 

结果将如ICS(API 14)上的下图所示:

此图像来自运行ICS的三星Galaxy S3。

注意:使用API 14 SO清单sdk启动的修改项目将是:

<uses-sdk
  android:minSdkVersion="11"
  android:targetSdkVersion="15" />

最后一句话:作为我在Android开发的小知识(我不是专家),

1-自定义警报对话框在API中平稳运行

如果我们想让它在ICS中运行,与API中显示的效果相同

2-即使在ICS的结果也不令人满意,圆角只适用于标题而不适用于页脚。

第二次更新:最后我得到了所有的角落,

只需将填充应用于对话框的页脚。xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <solid android:color="#7F7F7F" />
    <corners android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp" />
    <stroke android:color="#7F7F7F" android:width="1dp" />
    <padding android:left="10dp" android:top="10dp" android:right="10dp"
android:bottom="10dp" /> 
</shape>

输出图像:

此图像来自运行ICS的三星Galaxy S3。

 类似资料:
  • 下面的屏幕截图显示了对1的测试。我想使矩形外的组件的角完全透明 但是,当父面板上有红色背景(或任何非标准颜色)时,您可以看到这种方法的缺点。拐角默认为默认面板颜色(最容易在中看到)。 最终,我希望它能用于父容器中的非标准颜色,但它的部分灵感来自于我需要做什么才能用渐变绘制复制此组件? 有人知道如何让这些角落透明吗? 而是为JTextArea的内部填充设计的,带有背景图像(

  • 问题内容: 我试图通过使用Spannable String来更改我的字符串,使其中间带有数字的徽章。我可以通过设置BackGroundColorSpan突出显示适当的字母/数字,但是需要一些帮助使其更加漂亮。我希望圆角周围有一些填充物。 本文确实与我要执行的操作非常接近:AndroidSpannableString将背景设置为文本的一部分 由于资源与应用程序的交互方式,我确实需要将资源保留为Tex

  • 本文向大家介绍Android生成带圆角的Bitmap图片,包括了Android生成带圆角的Bitmap图片的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android生成带圆角的Bitmap图片。分享给大家供大家参考。具体如下: 有时候我们在开发Android应用时,会遇到圆角图片的问题,那么,我们如何在Android中用代码来生成圆角Bitmap图片呢?下面这段代码也许能够帮到你。 该

  • 问题内容: 我想创建一个带有圆角的UITabBar。有什么办法可以 使UITabBar具有圆角?它将采用第二 张图片的形状。 该应用程序从tableView开始。当用户点击主题时,它们将被发送 到tabBar控制器。 在此处输入图片说明 这是我想要的形状 -编辑 - - - 这是我的AppDelegate: 问题答案: 嘿,我使用了tabBar的简单属性,即backgroundImage。 因此,

  • 本文向大家介绍Android实现带描边的圆角图片,包括了Android实现带描边的圆角图片的使用技巧和注意事项,需要的朋友参考一下 利用学过的BitmapShader渲染类,我们来实现一个带描边的圆角图片。 具体实现: 用来显示自定义的绘图类的布局文件 res/layout/main.xml: 打开MainActivity,在文件中创建名为MyView的内部类,继承android.view.Vie

  • 问题内容: 我已经看到了很多代码,但是似乎没有一个代码可以很好地工作或根本无法工作。我已经使用了图片作为圆角,但是我需要代码,以使其能够围绕的边界。我针对此问题找到的唯一解决方案是在边框周围的单元格中具有图像。我还能尝试什么吗? 问题答案: 尝试: 这将在Firefox,Safari,Chrome和任何其他CSS3兼容浏览器中运行。为此创建一个单独的类可能会更容易-完全兼容需要3条语句。 另外,请