Android的对话 - 圆角和透明度 [英] Android Dialog - Rounded Corners and Transparency

查看:169
本文介绍了Android的对话 - 圆角和透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与圆角的自定义机器人对话。我现在的尝试,给了我这样的结果。

正如你所看到的,边角圆润,但它留下的白角仍然完好无损。

下面是我把在绘制文件夹与圆角的红色边框打造蓝色对话框中的XML。

 < XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目>
        <形状
            机器人:形状=矩形>
            [固体机器人:颜色=@色/ transparent_black/>
            <边角机器人:半径=@扪/ border_radius/>
        < /形状>
    < /项目>
    <项目
        机器人:左=@扪/ border_width
        机器人:右=@扪/ border_width
        机器人:顶部=@扪/ border_width
        机器人:底部=@扪/ border_width>

        <形机器人:形状=矩形>
            [固体机器人:颜色=@色/蓝色/>
            <边角机器人:半径=@扪/ border_radius/>
        < /形状>
    < /项目>
< /层列表>
 

下面是弹出窗口的布局。

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
风格=@风格/补
机器人:方向=垂直
机器人:layout_margin =@扪/ spacing_normal
机器人:填充=@扪/ spacing_normal

机器人:背景=@可绘制/ border_error_dialog>

< RelativeLayout的
    风格=@风格/块
    机器人:layout_gravity =中心>

    < ImageView的
        机器人:ID =@ + ID / imageView1
        风格=@风格/包
        机器人:layout_alignParentLeft =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:contentDescription =@字符串/ content_description_filler
        机器人:SRC =@可绘制/ ic_launcher/>

    <的TextView
        机器人:ID =@ + ID / textView1
        风格=@风格/ ERROR_TEXT
        机器人:layout_centerVertical =真
        机器人:layout_toRightOf =@ + ID / imageView1
        机器人:文本=@字符串/ error_login/>

< / RelativeLayout的>

<按钮
    机器人:ID =@ + ID /按钮1
    风格=@风格/包
    机器人:layout_gravity =中心
    机器人:文本=按钮/>

< / LinearLayout中>
 

和下面是我创建对话框的活动。

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    按钮B1 =(按钮)findViewById(R.id.button1);
    b1.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(MainActivity.this);

            查看孩子= getLayoutInflater()膨胀(R.layout.dialog_custom_tom,空)。
            alertDialogBu​​ilder.setView(子);

            AlertDialog alertDialog = alertDialogBu​​ilder.create();

            alertDialog.show();
        }
    });
}
 

解决方案

我发现的唯一的解决办法是这里。用对话代替AlertDialog并设置透明背景:
dialog.getWindow()setBackgroundDrawableResource(android.R.color.transparent);
因此,你不能使用生成器。但是你可以使用新的对话框()也DialogFragment的onCreateDialog回调,如果你遵循的最佳指南。

这工作也为姜饼。

除了层状提拉可以被简化为一个形状使用XML元素<中风>为边界。

I'm trying to make a custom android dialog with rounded corners. My current attempts have given me this result.

As you can see, the corners are rounded, but it leaves the white corner still intact.

Below is the xml that I put in the drawable folder to create the blue dialog with the red border with the rounded corners.

<?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>

Below is the layout of the dialog.

<?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>

And below is the Activity in which I create the dialog.

@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();
        }
    });
}

解决方案

The only solution I have found is here. Use Dialog instead of AlertDialog and set transparent background:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Therefore you can't use the builder. But you can use new Dialog() also in onCreateDialog callback of DialogFragment if you follow to best guidelines.

This works also for Gingerbread.

Besides the layered drawable can be simplified to one shape with xml element <stroke> for the border.

这篇关于Android的对话 - 圆角和透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆