赞
踩
自定义对话框是经常使用的功能,我们常用的弹窗操作,除了使用popwindow就是使用dialog来实现,这两种组件都支持之定义布局和功能来满足我们个性化的需求,也可以不采用自定义而直接使用系统封装好的api来实现功能。今天简单总结下在使用dialog做弹窗功能的方法和会遇到的问题与解决方案。
方法一:直接使用系统的,不自定义布局和功能方式
- /* @setIcon 设置对话框图标
- * @setTitle 设置对话框标题
- * @setMessage 设置对话框消息提示
- */
- final AlertDialog.Builder normalDialog =
- new AlertDialog.Builder(MainActivity.this);
- normalDialog.setIcon(R.drawable.icon_dialog);
- normalDialog.setTitle("我是一个普通Dialog")
- normalDialog.setMessage("你喜欢系统对话框吗?");
- normalDialog.setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- //...To-do
- }
- });
- normalDialog.setNegativeButton("关闭",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- //...To-do
- }
- });
-
- //如果想自定义三个按钮的对话框,可以把下面的方法注释打开
- // normalDialog.setNeutralButton("第三个按钮",
- // new DialogInterface.OnClickListener() {
- // @Override
- // public void onClick(DialogInterface dialog, int which) {
- // // ...To-do
- // }
- // });
- // 显示
- normalDialog.show();

方法二:采用自定义布局和功能方式
自定义对话框布局: high_opinion_dialog_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="wrap_content"
- android:paddingTop="@dimen/dp_20"
- android:paddingBottom="@dimen/dp_10"
- android:paddingLeft="@dimen/dp_15"
- android:paddingRight="@dimen/dp_15"
- android:orientation="vertical">
-
- <TextView
- android:text="Rate US"
- android:gravity="center"
- android:textSize="@dimen/sp_18"
- android:textColor="@color/black"
- android:layout_gravity="center_horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:text="We're glad you're enjoying using our app! Would you mind giving us a review?"
- android:gravity="center"
- android:textSize="@dimen/sp_12"
- android:layout_marginTop="@dimen/dp_5"
- android:textColor="@color/black"
- android:layout_gravity="center_horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_marginTop="@dimen/dp_15"
- android:layout_height="@dimen/dp_37">
-
- <Button
- android:id="@+id/btn_cancel_high_opion"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:text="Maybe later"
- android:background="@drawable/btn_cancer_high_opion_shape"
- android:textColor="@color/white"
- android:layout_weight="1"/>
-
- <View
- android:layout_width="@dimen/dp_20"
- android:layout_height="match_parent"
- />
- <Button
- android:id="@+id/btn_agree_high_opion"
- android:layout_width="0dp"
- android:text="Sure"
- android:textColor="@color/white"
- android:background="@drawable/btn_agree_high_opinion_shape"
- android:layout_height="match_parent"
- android:layout_weight="1"/>
-
- </LinearLayout>
-
-
-
- </LinearLayout>

然后在activity或者fragment中想要加点击弹出对话框的控件的监听事件中调用初始化下面方法
- public class HomeActivity extends AppCompatActivity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home);
-
- Button btn= findViewById(R.id.btn)
- //点击按钮弹出对话框
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showDialog();
- }
- });
-
- }
-
- //初始化并弹出对话框方法
- private void showDialog(){
- View view = LayoutInflater.from(this).inflate(R.layout.high_opinion_dialog_layout,null,false);
- final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();
-
- Button btn_cancel_high_opion = view.findViewById(R.id.btn_cancel_high_opion);
- Button btn_agree_high_opion = view.findViewById(R.id.btn_agree_high_opion);
-
- btn_cancel_high_opion.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- SharedPreferencesUnitls.setParam(getApplicationContext(),"HighOpinion","false");
- //... To-do
- dialog.dismiss();
- }
- });
-
- btn_agree_high_opion.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //... To-do
- dialog.dismiss();
- }
- });
-
- dialog.show();
- //此处设置位置窗体大小,我这里设置为了手机屏幕宽度的3/4
- dialog.getWindow().setLayout((ScreenUtils.getScreenWidth(this)/4*3),LinearLayout.LayoutParams.WRAP_CONTENT);
- }
-
- }

此处附上ScreenUtils工具类代码
- public class ScreenUtils {
-
- /**
- * 获取屏幕高度(px)
- */
- public static int getScreenHeight(Context context) {
- return context.getResources().getDisplayMetrics().heightPixels;
- }
- /**
- * 获取屏幕宽度(px)
- */
- public static int getScreenWidth(Context context) {
- return context.getResources().getDisplayMetrics().widthPixels;
- }
-
- }

需要注意的问题总结:系统的dialog的宽度默认是固定的,即使你自定义布局的怎么修改宽高也不起作用,如果想修改弹出窗体大小,可以使用下面这段代码在调用dialog.show()方法之后来实现改变对话框的宽高的需求
- //此处设置位置窗体大小,
- dialog.getWindow().setLayout(width,height);
今天的分享先到这里,后续会不断添加和更新更多更好的学习资料,如果你喜欢可以关注加好友,互相探讨学习,我们互相学习一起成长!
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。