android弹窗对话框中间使用自定义view

有时需要使用AlertDialog来弹窗,此时方便控制弹窗的主题,比如title和按钮都是统一的;

 

如果中间需要显示的view不同,比如显示下图:

应该怎么做呢,思路就是替换message使用view,这里使用了组合迭代模式哦;

Builder builder = new AlertDialog.Builder(
		new ContextThemeWrapper(this, R.style.dialog_theme_custom));
View view = LayoutInflater.from(this).inflate(R.layout.about_alertdialog_icon,null);  //关键语句1,你自定义的layout,所有layout哟

String versionString = this.getString(R.string.about_app_version, vc); //操作你自定义view里的值,也可以没有这三句
TextView tv = (TextView) view.findViewById(R.id.tv_about_appversion);
tv.setText(versionString);

builder.setView(view);  //关键语句2,令自定义view作为alertDialog的子view,此时

//builder.setMessage(msg); //如果完全使用layout里的view显示,则注掉msg吧;

Dialog dialog = builder.create();
dialog.show();

显示结果如下,邮件和网址可以点击,原因是TextView中设置了 android:autoLink="email" ,也可以设置为web哦;

以下是大段的layout.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:orientation="horizontal" >

    <ImageView
        android:id="@+id/ic_about_logo"
        android:layout_width="76dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="26dp"
        android:scaleType="fitStart"
        android:src="@drawable/ic_lefttop_app" />
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="26dp"
        android:layout_marginLeft="18dp"
        android:orientation="vertical">
        
        <TextView
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/app_name"
            android:textSize="@dimen/text_font_size_about_app_title"
            android:textColor="@color/black"
            />
        <TextView
            android:id="@+id/tv_about_appversion"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/about_app_version"
            android:textSize="@dimen/text_font_size_about_text"
            android:textColor="@color/black"
            />
        <TextView
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" 
            android:text="@string/about_app_rights_claim"
            android:textSize="@dimen/text_font_size_about_text"
            android:textColor="@color/black"
            android:autoLink="web"
            />
        <TextView
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" 
            android:paddingBottom="20dp" 
            android:text="@string/about_app_feedback"
            android:textSize="@dimen/text_font_size_about_text"
            android:textColor="@color/black"
            android:autoLink="email"
            />
        
        
    </LinearLayout>

</LinearLayout>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值