如何在2.2到4.4以及所有平板电脑的所有版本中使对话框看起来相同

时间:2014-03-21 06:57:16

标签: android dialog android-alertdialog

如何在2.2到4.4以及所有平板电脑的所有版本中使对话框看起来相同。我创建了4.2中看起来完美的对话框,我也希望在2.3中看到相同的对话框。如何使对话框看起来一样版本从2.2到4.4以及所有平板电脑。我创建的对话框在4.2中看起来很完美,我也希望在2.3中看到相同的对话框。

public class MainActivity extends Activity {
private Context context;
private TextView textView;
private Dialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = MainActivity.this;
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            MainActivity.this);
    LayoutInflater inflater = MainActivity.this.getLayoutInflater();
    alertDialog.setView(inflater.inflate(R.layout.dialog_textview, null));



    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("Call",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    // Write your code here to invoke YES event
                    if (deviceIsTablet(context)) { // if device is a
                        // tablet
                        if (isAppInstalled("com.skype.raider")) {
                            // insert what you want to do if device has
                            // skype
                            Intent intent = new Intent(
                                    "android.intent.action.VIEW");
                            intent.setData(Uri.parse("skype:" + 123456789));
                            startActivity(intent);
                        } else {
                            // connect to Play store or whatever
                            startActivity(new Intent(
                                    Intent.ACTION_VIEW,
                                    Uri.parse("http://play.google.com/store/apps/details?id=com.skype.raider")));
                        }
                    } else { // if device is not a tablet
                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("tel:123456789"));
                        startActivity(callIntent);
                    }
                }
            });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to invoke NO event

                }
            });

    // Showing Alert Message
    alertDialog.show();

}

// determine whether or no the android device is a tablet or not
public static boolean deviceIsTablet(Context c) {
    Resources res = c.getResources();
    return (res.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

private boolean isAppInstalled(String appToCheck) {
    PackageManager pm = getPackageManager();
    boolean isOnDevice = false;
    try {
        pm.getPackageInfo(appToCheck, PackageManager.GET_ACTIVITIES);
        isOnDevice = true;
    } catch (PackageManager.NameNotFoundException exception) {
        isOnDevice = false;
    }
    return isOnDevice;
}

}

1 个答案:

答案 0 :(得分:0)

检查以下链接以创建自定义对话框:

创建自定义对话框的步骤:

  • 创建布局文件(例如dialog.xml)
  • 使用您的活动引用您的dialog.xml类。
  • 使用对话框视图捆绑新布局文件。
  • 显示对话框。