安卓面试

Android 自定义底部圆角弹窗的实现

2019-10-21  本文已影响0人  飞指

直接上代码:
布局代码 1.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="20dp">


    <Button
        android:id="@+id/call_phone"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/ok_to_pay"
        android:text="呼叫 13244669955"
        android:textColor="@color/white"
        android:textSize="16sp" />

    <Button
        android:id="@+id/cancel_btn"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/ok_to_pay"
        android:text="取消"
        android:textColor="@color/white"
        android:textSize="16sp" />

</LinearLayout>

Java实现代码

 View contentView = getLayoutInflater().inflate(R.layout.contact_to_merchant, null);
        final Dialog dialog = new Dialog(this, R.style.custom_dialog2_);
        dialog.setContentView(contentView);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.drawable.dialog_top_radios);
        dialog.show();

背景样式代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp" />
    <solid android:color="@color/white" />

</shape>

样式代码


    <style name="custom_dialog2_" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!-- Dialog的windowFrame框为无 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否漂现在activity上 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 是否半透明 -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@null</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowContentOverlay">@null</item>
        <!-- 去除黑色边框的关键设置项 -->
        <item name="android:backgroundDimEnabled">true</item>
    </style>
上一篇 下一篇

猜你喜欢

热点阅读