Android 自定义Dialog

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

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

先贴出Activity的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="40dp"
        android:paddingTop="40dp"
        android:src="@mipmap/ic_launcher_round" />

    <TextView
        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" />

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingBottom="20dp">

        <TableRow>

            <Button
                android:id="@+id/button01"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:text="@string/disagree"
                android:textColor="@android:color/holo_blue_bright" />

            <Button
                android:id="@+id/button02"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:text="@string/agree"
                android:textColor="@android:color/holo_blue_bright" />
        </TableRow>

    </TableLayout>

</LinearLayout>

预览中的Layout:

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的形式显示,只需要:

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

<activity android:name=".MainActivity"
  android:theme="@android:style/Theme.Material.Dialog">

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

以Dialog形式显示的Activity效果图

诸君共勉:)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在项目中,我们会遇到各种各样的界面需求,比如对话框和选择框,都是会配合具体项目的UI界面来做,而不是说用自带的弹出...
    青蛙要fly阅读 38,436评论 32 179
  • 在开发中,我们经常会用到一些自定义布局的对话框。每次自己写的时候,经常会遇到一些细小的问题需要上网来解决,所以干脆...
    黑猫警长_01阅读 14,016评论 1 37
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,851评论 0 17
  • 这是80后的传记,让我深思。我又何尝不是如此! 死亡,我思考了很久,害怕什么?害怕见不到亲人
    金悦JY阅读 1,319评论 0 0
  • 文|水聽 就这三言两语, 明显心不在焉, 去掉成何体统, 简直一派胡言, 此等吊儿郎当, 还想吃饭看电视剧, 做梦...
    水聽阅读 1,508评论 3 8