如何创建一个双色对话框这样吗? [英] How do I create a two-color dialog like this?

查看:129
本文介绍了如何创建一个双色对话框这样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个对话框样式像下面,但我有点卡住了。它具有圆角和两个不同的背景色。

这将包含中端垂直设置多个Textviews。我试图做一个垂直的LinearLayout包含也是垂直的LinearLayout两个孩子,但这似乎没有过去好。

如何创建这样的图,其中背景的两种颜色使用相同的圆角,并且可以包含多个垂直件每

在这里输入的形象描述

我目前的code看起来是这样的。我给自己定一个单一的垂直布局,它采用了圆润,边角白色背景填充,我设置的红色背景前两个文本的意见,因为他们需要的是白色的红色。然而,他们的背景不能推到他们的父母,因为填充的边缘。

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =600dp的
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:背景=@绘制/ warning_dialog_background
    机器人:填充=20dp
    风格=@风格/对话框>    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=CENTER_HORIZONTAL
        机器人:背景=@彩色/警告
        风格=@风格/白
        机器人:文字=@字符串/ warning_block_explanation
        机器人:paddingBottom会=30dp
        />    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=CENTER_HORIZONTAL
        风格=@风格/ title.warning
        机器人:文字=@字符串/ warning_block_warning_title
        />
    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=CENTER_HORIZONTAL
        风格=@风格/ safe.title
        机器人:文字=@字符串/ safe_title
        />
    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=CENTER_HORIZONTAL
        风格=@风格/安全
        机器人:文字=@字符串/ safe_text
        />
< / LinearLayout中>


解决方案

您可以创建自定义的布局和使用对话的setContentView()膨胀该布局;

 自定义对话框;        最后对话的对话=新的对话框(背景);        dialog.setContentView(R.layout.custom);        dialog.setTitle(标题...);        //设置自定义对话框组件 - 文字,图片和按钮
        TextView的文本=(TextView的)dialog.findViewById(R.id.text);
        text.setText(Android的定制对话框的例子!);
        ImageView的图像=(ImageView的)dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);        按钮dialogBu​​tton =(按钮)dialog.findViewById(R.id.dialogBu​​ttonOK);
        //如果点击按钮,关闭对话框定制
        dialogBu​​tton.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                dialog.dismiss();
            }
        });        dialog.show();

布局这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直的android:layout_width =match_parent
机器人:layout_height =match_parent>
<的TextView
    机器人:layout_width =match_parent
    机器人:layout_height =70
    机器人:文字=白色文字放在这里
    机器人:比重=中心
    机器人:文字颜色=#FFF
    机器人:layout_marginRight =5DP
    机器人:layout_marginLeft =5DP
    机器人:layout_marginTop =5DP
    机器人:TEXTSIZE =18dp
    机器人:背景=@绘制/ custom_drwable/>
<的TextView
    机器人:layout_marginRight =5DP
    机器人:layout_marginLeft =5DP
    机器人:layout_width =match_parent
    机器人:layout_height =120
    机器人:文字=红色文本放在这里
    机器人:比重=中心
    机器人:文字颜色=#F2122B
    机器人:TEXTSIZE =18dp
    机器人:背景=#FFF/>< / LinearLayout中>

对于绘制相同的:

 <?XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
[固体机器人:颜色=#F2122B/>
<角落的an​​droid:topLeftRadius =4DP
    机器人:topRightRadius =4DP/>< /形状>

I'm looking to create a Dialog styled like the below, but I'm a little stuck. It has rounded corners and two different background colors.

It will contain multiple Textviews in a vertical setup in the end. I tried to make a vertical LinearLayout contain two children that were also vertical LinearLayout, but that did not seem to go over well.

How do you create a view like this, with two colors of background that use the same rounded corners and can contain multiple, vertical items each?

My current code looks like this. I've set a single vertical layout, which uses a rounded-corners white background with padding, and I set the red background on the first two text-views, since they need to be white-on-red. However, their backgrounds can't push out to the edges of their parent because of the padding.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="600dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/warning_dialog_background"
    android:padding="20dp"
    style="@style/dialog" >

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:background="@color/Warning"       
        style="@style/white"
        android:text="@string/warning_block_explanation"
        android:paddingBottom="30dp"
        />

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"        
        style="@style/title.warning"
        android:text="@string/warning_block_warning_title"
        />
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"        
        style="@style/safe.title"
        android:text="@string/safe_title"
        />
    <TextView         
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"        
        style="@style/safe"
        android:text="@string/safe_text"        
        />
</LinearLayout>

解决方案

You can create custom layout and inflate that layout using setContentView() for dialog;

        custom dialog;

        final Dialog dialog = new Dialog(context);

        dialog.setContentView(R.layout.custom);

        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

Layout for this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="70"
    android:text="White Text Goes Here"
    android:gravity="center"
    android:textColor="#fff"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:textSize="18dp"
    android:background="@drawable/custom_drwable"/>
<TextView
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_width="match_parent"
    android:layout_height="120"
    android:text="Red Text Goes Here"
    android:gravity="center"
    android:textColor="#F2122B"
    android:textSize="18dp"
    android:background="#fff"/>

</LinearLayout>

drawable for same:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#F2122B"/>
<corners android:topLeftRadius="4dp"
    android:topRightRadius="4dp"/>

</shape>

这篇关于如何创建一个双色对话框这样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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