Android 中的AlertDialog使用自定义布局

#Android使用指定的View开发弹窗功能 Android开发中进程会使用到咱们的AlertDialog,可是比较惋惜的是咱们的Android原生的AlertDialog的效果又比较的简陋,这个时候咱们就须要自定义的View去显示了。 ##Overview Android 中的AlertDialog 的效果十分的的炫酷。 他的效果是相似弹窗的效果。 那么咱们就能够拿他来作不少事情,好比点击添加时,弹出咱们的AlertDialog 的效果就十分的棒。那就让咱们看一下如何使用它。android

##首先 咱们看一下 咱们的 自定义的view 的布局。app

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="400dp"
    android:layout_height="470dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="20dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/editTodoItems"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="8"
                android:hint="今天打算作什么?"
                android:textColor="#000000"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/TodotxtAddItems"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="center"
                android:text="添加"
                android:textColor="@color/MainColor"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <androidx.cardview.widget.CardView

                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:backgroundTint="@color/dark"
                app:cardCornerRadius="20dp">

                <LinearLayout
                    android:id="@+id/today"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginRight="15dp"
                        android:layout_marginBottom="10dp"
                        android:text="今天"
                        android:textColor="#fff"
                        android:textSize="12sp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:layout_marginLeft="10dp"
                android:backgroundTint="@color/dark"
                app:cardCornerRadius="20dp">

                <LinearLayout
                    android:id="@+id/tomorrow"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginRight="15dp"
                        android:layout_marginBottom="10dp"
                        android:text="明天"
                        android:textColor="#fff"
                        android:textSize="12sp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:layout_marginLeft="10dp"
                android:backgroundTint="@color/dark"
                app:cardCornerRadius="20dp">

                <LinearLayout
                    android:id="@+id/selectDay"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/Day3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginRight="15dp"
                        android:layout_marginBottom="10dp"
                        android:gravity="center"
                        android:text="选择日期"
                        android:textColor="#fff"
                        android:textSize="12sp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:layout_marginLeft="10dp"
                android:backgroundTint="@color/dark"
                app:cardCornerRadius="20dp">

                <LinearLayout
                    android:id="@+id/intoBox"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginRight="15dp"
                        android:layout_marginBottom="10dp"
                        android:text="🗑放入待办箱"
                        android:textColor="#fff"
                        android:textSize="12sp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="⏰"
                android:textSize="19sp" />

            <EditText
                android:id="@+id/editTodoItemsMsg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="添加备注"
                android:textSize="14sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="❗工做量 "
                android:textSize="18sp" />

            <Button
                android:id="@+id/ordinary"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="通常"
                android:textColor="#fff"
                android:textSize="16sp" />

            <Button
                android:id="@+id/medium"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="中等难度"
                android:textColor="#fff"
                android:textSize="16sp" />

            <Button
                android:id="@+id/higher"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:text="较高难度"
                android:textColor="#fff"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

这样咱们的 自定义的View 就建立完成了。 而后咱们看一下如何去使他。 首先咱们看一下 在AlertDialog 中 使用 SetView 的方法:布局

View view = LayoutInflater.from(TodoActivity.this).inflate(R.layout.todo_add_items, null, false);

首先咱们使用咱们的View 去接收咱们的自定义布局。 而后咱们建立咱们的AlertDialog。 而后咱们直接进行.setView(view);便可。this


##看一下效果 code

相关文章
相关标签/搜索