1,自定义一个dialog类,如下:在activity中调用showMyDialog()方法,就可以自定义自己的dailog
public class ShowAllMyDialog {
public static Dialog mdialog;
/**
* when check the user info, show the user's info dialog
* */
public static void showMyDialog(BeanUserData userData, Context context) {
mdialog = new Dialog(context, R.style.CardDialogStyle);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.start_dialog, null);
TextView username = (TextView) v.findViewById(R.id.username);
TextView userage = (TextView) v.findViewById(R.id.userage);
username.setText(userData.getUserName());
userage .setText(userData.getUserAge());
/* content.setText(s);
content.setGravity(Gravity.CENTER);*/
mdialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mdialog.getWindow().setContentView(v);//自定义布局应该在这里添加,要在dialog.show()的后面
// mdialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
// 这里是指dialog显示的时候 点击其他空白地方是否将dialog隐藏 根据需求自己设置
mdialog.setCancelable(true);
mdialog.show();
WindowManager.LayoutParams attrs = mdialog.getWindow().getAttributes();
attrs.width = 500;// attrs.width =580;
attrs.height = 380;// attrs.height = 600;
// if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
// v.setSystemUiVisibility(View.GONE);
// } else if (Build.VERSION.SDK_INT >= 19) {
// //for new api versions.
// // View decorView = getWindow().getDecorView();
// int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
// | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
// v.setSystemUiVisibility(uiOptions);
// }
attrs.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View
.SYSTEM_UI_FLAG_IMMERSIVE;
mdialog.getWindow().setAttributes(attrs);
}
public static void dismissDialog() {
if (mdialog != null && mdialog.isShowing()) {
mdialog.dismiss();
mdialog = null;
}
}
}
2,在values文件夹下新增一个style.xml文件。新增一个style
<style name="CardDialogStyle" parent="@android:style/Theme.Dialog" >
<!-- 背景 -->
<item name="android:windowBackground">@drawable/bg</item>
</style>
3,新增一个layout文件,为自定义的dialog布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:background="@drawable/content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:singleLine="true"
android:text="姓 名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/username"
android:paddingLeft="20dp"
android:layout_width="225dp"
android:layout_height="35dp"
android:background="@drawable/input"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:textSize="18sp"
android:singleLine="true"
android:text=""
android:inputType="textMultiLine"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:orientation="horizontal">
<TextView
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:singleLine="true"
android:text="年 龄:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/userage"
android:paddingLeft="20dp"
android:layout_width="225dp"
android:layout_height="35dp"
android:background="@drawable/input"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:textSize="18sp"
android:singleLine="true"
android:text=""
android:inputType="textMultiLine"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
温故而知新!