如何创建这样的双色对话框?

时间:2015-12-02 11:59:50

标签: android dialog

我希望创建一个像下面这样的Dialog,但我有点卡住了。它有圆角和两种不同的背景颜色。

最后,它将在垂直设置中包含多个Textview。我试图让一个垂直的LinearLayout包含两个也是垂直LinearLayout的子节点,但这似乎没有顺利完成。

如何创建这样的视图,两种颜色的背景使用相同的圆角,每个颜色可以包含多个垂直项目?

enter image description here

我当前的代码看起来像这样。我设置了一个垂直布局,使用带有填充的圆角白色背景,并在前两个文本视图中设置红色背景,因为它们需要是白色的红色。但是,由于填充,它们的背景无法推向其父级的边缘。

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

1 个答案:

答案 0 :(得分:1)

您可以使用setContentView()创建自定义布局并使用对话框扩展该布局;

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

此布局:

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