Android自定义对话框的实现

自定义对话框的思路就是编写对话框的布局文件xml,然后在对话框中显示不同的控件。以下以显示文本控件为例(ImageView等都可以显示)。

1.布局文件connect_dlg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_height="wrap_content" 
	android:layout_width="fill_parent"
	android:background="#ffffffff"
	 android:orientation="vertical"
	android:id="@+id/llToast" >
	<TextView
		android:layout_height="wrap_content"
		android:layout_margin="1dip"
		android:textColor="#ffffffff"
		android:layout_width="fill_parent"
		android:gravity="center"
		android:textSize="16sp"
		android:background="#FF129de2"
		android:id="@+id/tvTitleToast" />
	<LinearLayout
		android:layout_height="wrap_content"
		android:orientation="vertical"
		android:id="@+id/llToastContent"
		android:layout_marginLeft="1dip"
		android:layout_marginRight="1dip"
		android:layout_marginBottom="1dip"
		android:layout_width="wrap_content"
		android:padding="15dip"
		android:background="#FFFFFFFF" >
		<TextView
			android:layout_height="wrap_content"
			android:paddingRight="10dip"
			android:paddingLeft="10dip"
			android:layout_width="wrap_content"
			android:gravity="center"
			android:textSize="16sp"
			android:textColor="#FFff6699"
			android:id="@+id/tvTextToast" />
	</LinearLayout>
	<LinearLayout
            android:id="@+id/MyLayout_ad2"
            android:orientation="horizontal" 
            android:layout_width="fill_parent"
            android:layout_height="40sp">
              <com.tencent.exmobwin.banner.TAdView
                   android:id="@+id/adview2"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:gravity="top|right" >
               </com.tencent.exmobwin.banner.TAdView>
       </LinearLayout>
</LinearLayout>


2.编写显示对话框函数。ShowConnectDialog(String textString)

private void ShowConnectDialog(String textString) {
		LinearLayout loginLayout1 = (LinearLayout) getLayoutInflater().inflate(
				R.layout.connect_dlg, null);
		// adView.
		TextView title = (TextView) loginLayout1
				.findViewById(R.id.tvTitleToast);
		title.setText("系统提示");
		TextView text1 = (TextView) loginLayout1.findViewById(R.id.tvTextToast);
		text1.setText(textString);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(loginLayout1);
		builder.setPositiveButton("下载MobCtrl服务器?", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				//处理确定按钮

			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 处理取消按钮
				finish();
			}
		});
		builder.create().show();
	}


3.显示对话框。在需要显示的地方调用即可。

ShowConnectDialog("连接超时,请检查服务器是否开启及IP地址是否输入正确。确保电脑和手机连接在同一个网络内。");


4.效果


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值