对话框Dialog使用总结

本文详细介绍了如何在Android中使用Dialog,从创建Dialog到设置标题、内容、布局结构、背景、位置以及消除黑色边框等,提供了一套完整的Dialog定制方法。
摘要由CSDN通过智能技术生成

在项目中,我们经常用到对话框,这里我直接介绍AlertDialog的基础类Dialog,因为在我们定制化对话框都是Dialog的。

(1) Dialog的创建

Dialog dlgShare = new Dialog(this);

dlgShare.show();

如果就如上仅仅new一个Dialog这样显示出的dialog是没有任何内容的。


(2) 设置Dialog的标题

dlgShare.setTitle("Test");

如果仅仅只设置了标题而没有设置内容那么对话框只显示个标题,如下:



(3)  设置对话框的内容

Dialog内容设置主要通过以下两个方法:

public void setContentView(int layoutResID) 
public void setContentView(View view) 


Eg:

Dialog dlgShare = new Dialog(this);
dlgShare.setContentView(R.layout.dialog_share);
dlgShare.setTitle("Test");

dialog_share.xml文件内容如下

<?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="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#249EF6" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp" >
        <TextView
            android:layout_width=
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值