做一个 app自动更新功能,用到一个带圆角的Dialog
if (dialog == null) {
dialog = new Dialog(context, R.style.appupdate_dialog);
View v = View.inflate(context, R.layout.view_appupdate_dialog, null);
dialog.setContentView(v);
tvContent = ((TextView) v.findViewById(R.id.appupdatedialog_content));
btnYes = (Button) v.findViewById(R.id.appupdatedialog_yes);
btnNo = (Button) v.findViewById(R.id.appupdatedialog_no);
btnYes.setOnClickListener(this);
btnNo.setOnClickListener(this);
//获取屏幕尺寸,并设置自定义view的宽度,这样才能显示出view的背景圆角(原因不知道)
int[] arr = Util.getScreenPx(context);
LinearLayout bg = (LinearLayout) v.findViewById(R.id.appupdate_bg);
bg.setLayoutParams(new FrameLayout.LayoutParams((int) (arr[0] * 0.85),
FrameLayout.LayoutParams.WRAP_CONTENT));
}
dialog.show()
style文件下的R.style.appupdate_dialog<style name="appupdate_dialog" parent="Theme.AppCompat.Dialog"> <!--边框--> <item name="android:windowFrame">@null</item> <!--是否浮现在activity之上--> <item name="android:windowIsFloating">true</item> <!--半透明--> <item name="android:windowIsTranslucent">true</item> <!--无标题--> <item name="android:windowNoTitle">true</item> <!--背景颜色:透明--> <item name="android:windowBackground">@android:color/transparent</item> <!--背景模糊--> <item name="android:backgroundDimEnabled">true</item> </style>
xml文件R.layout.view_appupdate_dialog如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/appupdate_bg" android:layout_width="280dp" android:layout_height="wrap_content" android:background="@drawable/shape_dialog" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:gravity="center" android:text="发现新版本" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#008bea" /> <View android:layout_width="fill_parent" android:layout_height="2dp" android:background="#008bea" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/appupdatedialog_content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="10dp" android:focusable="true" android:textColor="#000" /> </LinearLayout> </ScrollView> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <Button android:id="@+id/appupdatedialog_yes" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="5dp" android:layout_weight="1" android:background="#008bea" android:gravity="center" android:text="立即更新" android:textColor="#ffffff" /> <Button android:id="@+id/appupdatedialog_no" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="5dp" android:layout_weight="1" android:background="#c2c2c2" android:gravity="center" android:text="以后再说" android:textColor="#000000" /> </LinearLayout> </LinearLayout>