android 验证码dialog,Android 自定义Dialog

本文介绍了如何将一个Android Activity显示为Dialog样式,只需在清单文件中设置Activity的主题为`Theme.Material.Dialog`。内容包括展示Activity的布局文件、主要组件以及在MainActivity中绑定按钮监听器。通过这种方法,可以实现更直观的对话框效果。

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

如果想让对话框具有自定义布局,可以先创建一个布局,然后通过调用 AlertDialog.Builder 对象上的 setView()将其添加到 AlertDialog。

但是我们也可以直接将一个Activity显示为Dialog。个人认为,这样的方法要更直观一些。

先贴出Activity的布局文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingBottom="40dp"

android:paddingTop="40dp"

android:src="@mipmap/ic_launcher_round" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:paddingBottom="20dp"

android:paddingLeft="20dp"

android:paddingRight="20dp"

android:paddingTop="20dp"

android:text="@string/texts_in_TextView" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:paddingBottom="20dp">

android:id="@+id/button01"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:text="@string/disagree"

android:textColor="@android:color/holo_blue_bright" />

android:id="@+id/button02"

android:layout_width="150dp"

android:layout_height="wrap_content"

android:text="@string/agree"

android:textColor="@android:color/holo_blue_bright" />

预览中的Layout:

932038342d22

Layout预览

import android.app.Activity;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button01);

// 为按钮绑定事件监听器

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

// 结束该Activity

finish();

}

});

}

}

在MainActivity中只是象征性地为其中一个按钮绑定了监听器。

而要实现把Activity以Dialog的形式显示,只需要:

在 清单文件元素中将其主题设置为

android:theme="@android:style/Theme.Material.Dialog">

就这么简单。Activity 便会显示在一个对话框窗口中,而非全屏显示。

效果图:

932038342d22

以Dialog形式显示的Activity效果图

诸君共勉:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值