有时需要使用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>