具有自定义形状的DialogFragment

时间:2017-01-31 12:08:51

标签: android android-layout android-custom-view android-dialogfragment android-viewgroup

如何使用自定义形状创建对话框片段?

我有一个图标需要放在对话框窗口的顶部(带透明度)。

Image

对话框本身应为:

Dialog screen

对话框布局:

<FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:layout_gravity="bottom|center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/server_error_title"
                android:layout_marginTop="80dp"
                android:layout_centerHorizontal="true"
                android:textSize="20sp"
                android:textAllCaps="true"
                android:text="@string/server_error_title"
                android:textColor="@color/soft_blue"
                android:fontFamily="sans-serif-medium"/>
            <View
                android:layout_width="60dp"
                android:layout_height="2dp"
                android:id="@+id/server_error_divider"
                android:layout_marginTop="21dp"
                android:layout_centerHorizontal="true"
                android:layout_below="@id/server_error_title"
                android:background="@color/soft_blue" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/server_error_message"
                android:layout_marginTop="19dp"
                android:layout_centerHorizontal="true"
                android:layout_below="@id/server_error_divider"
                style="@style/TextDialog"
                android:text="@string/server_error_message"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:id="@+id/server_error_large_divider"
                android:layout_marginTop="51dp"
                android:layout_below="@id/server_error_message"
                android:background="@color/greyish"/>
           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:gravity="center_horizontal"
               android:background="@color/white_grey"
               android:layout_below="@id/server_error_large_divider">
               <Button
                   android:layout_width="320dp"
                   android:layout_height="46dp"
                   android:id="@+id/server_error_close_btn"
                   android:layout_marginTop="30dp"
                   android:layout_marginBottom="30dp"
                   style="@style/TextButton2"
                   android:text="@string/server_error_close_btn"
                   android:background="@drawable/big_button_shape" />
           </LinearLayout>
        </RelativeLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/error_dialog_icon"
            android:src="@drawable/ic_error_circle"
            android:layout_gravity="center|top"/>
    </FrameLayout>

但我得到的是:

enter image description here

我是否需要自定义ViewGroup,或者可以更轻松地完成它?

4 个答案:

答案 0 :(得分:7)

您可以根据自己的想法将override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if (segue.identifier == "toDetail" ) { print("Segue toDetail") let theDestination : StationController = segue.destination as! StationController theDestination.LabelText = (sender as! SubMGLAnnotation).title! } } 放在Views(custom button, layout)内。

对话方法,使LinearLayout透明:

Window

test.xml

Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.test);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();  

结果(避免BG)

enter image description here

答案 1 :(得分:4)

使用以下xml示例代码:

<FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="45dp"
            android:background="@android:color/white"
            android:layout_gravity="bottom|center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/server_error_title"
                android:layout_marginTop="80dp"
                android:layout_centerHorizontal="true"
                android:textSize="20sp"
                android:textAllCaps="true"
                android:text="@string/server_error_title"
                android:textColor="@color/soft_blue"
                android:fontFamily="sans-serif-medium"/>
            <View
                android:layout_width="60dp"
                android:layout_height="2dp"
                android:id="@+id/server_error_divider"
                android:layout_marginTop="21dp"
                android:layout_centerHorizontal="true"
                android:layout_below="@id/server_error_title"
                android:background="@color/soft_blue" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/server_error_message"
                android:layout_marginTop="19dp"
                android:layout_centerHorizontal="true"
                android:layout_below="@id/server_error_divider"
                style="@style/TextDialog"
                android:text="@string/server_error_message"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:id="@+id/server_error_large_divider"
                android:layout_marginTop="51dp"
                android:layout_below="@id/server_error_message"
                android:background="@color/greyish"/>
           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:gravity="center_horizontal"
               android:background="@color/white_grey"
               android:layout_below="@id/server_error_large_divider">
               <Button
                   android:layout_width="320dp"
                   android:layout_height="46dp"
                   android:id="@+id/server_error_close_btn"
                   android:layout_marginTop="30dp"
                   android:layout_marginBottom="30dp"
                   style="@style/TextButton2"
                   android:text="@string/server_error_close_btn"
                   android:background="@drawable/big_button_shape" />
           </LinearLayout>
        </RelativeLayout>

        <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="4dp"
        android:background="@drawable/error_circle">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/error_dialog_icon"
            android:src="@drawable/ic_error_circle"
            android:layout_gravity="center|top"/>

        </FrameLayout>
    </FrameLayout>

和error_circle:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="oval"
    android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="45dp"
        android:color="@android:color/white" />
</shape>

修改

在DialogFragment的onCreateView()中:

getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

答案 2 :(得分:1)

创建样式并将形状设置为背景,例如:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="my-textbox">
  <img class="icon" src="http://directorzcut.com/modules/aqb/profile_mp3_player/templates/base/images/icons/popout.png" />
  <input type="text" class="text-box" />
</div>
<p class="text-here"></p>

答案 3 :(得分:1)

尝试这样,它会显示透明对话框

[1485859289] => Array
    (
        [my_hourly_event] => Array
            (
                [40cd750bba9870f18aada2478b24840a] => Array
                    (
                        [schedule] => hourly
                        [args] => Array
                            (
                            )

                        [interval] => 3600
                    )

            )

    )