自定义弹出框

本文介绍如何在Android应用中创建自定义样式的对话框,包括使用AlertDialog.Builder进行布局设置、按钮点击事件处理等,帮助开发者提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
             android:text="显示通知"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:id="@+id/button"
             android:onClick="show1" />
</RelativeLayout>




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.button);
}

public void show1(View v){
     View inflate = View.inflate(MainActivity.this, R.layout.a, null);
    View viewById = inflate.findViewById(R.id.buttons);
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     builder.setView(inflate);
    final AlertDialog alertDialog = builder.create();
    viewById.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog.dismiss();
            down();
        }
    });
    alertDialog.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:text="正在下载"
        android:textColor="#000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/ss"
        android:text="dddd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttons"
        />

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

    <solid android:color="#ffffff" />
    <stroke
        android:width="0.8dp"
        android:color="#ffffff" />
    <!-- 圆角 -->
    <corners android:radius="6dp" />

</shape>
<?xml version="1.0" encoding="utf-8"?>
<resources>


    <style name="DialogTheme" parent="android:style/Theme.Dialog">
        <!--是否浮现在activity之上-->
        <item name="android:windowIsFloating">true</item>
    </style>

    <style name="DialogTheme.Transparent" parent="DialogTheme">
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

    <style name="DialogTheme.Transparent.NoTitle" parent="DialogTheme.Transparent">
        <item name="android:windowNoTitle">true</item>
    </style>

    <style name="DialogTheme.MyDialog" parent="DialogTheme.Transparent.NoTitle">
        <!--是否模糊-->
        <item name="android:backgroundDimEnabled">true</item>
    </style>


</resources>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值