0

因为我们知道alertdialog.builder.setview函数在API 21介绍允许我们设置我们的布局。有没有替代的方式来实现这一点,我也不想用support library enter image description here替代alertdialog.builder.setview以下API 21

这是我想创建

听到的是代码

,这是datetime_dialog.xml文件

Java文件的
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <TimePicker 
     android:id="@+id/timePicker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <DatePicker 
     android:id="@+id/datePicker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:calendarViewShown="false" /> 

</LinearLayout> 

代码

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
     MainActivity.this, AlertDialog.THEME_HOLO_DARK); 
alertDialogBuilder.setView(R.layout.datetime_dialog); 
alertDialogBuilder.setPositiveButton("Done", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       // go to a new activity of the app 
       Intent positveActivity = new Intent(
         getApplicationContext(), 
         MainActivity.class); 
       startActivity(positveActivity); 
      } 
     }); 
// set negative button: No message 
alertDialogBuilder.setNegativeButton("Cancel", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       // cancel the alert box and put a Toast to the 
       // user 
       dialog.cancel(); 

      } 
     }); 

AlertDialog alertDialog = alertDialogBuilder.create(); 
// show alert 
alertDialog.show(); 

我想支持这个高达API 9是有可能

回答

0

您需要先创建一个从塔布局来看,有:

View v = LayoutInflater.inflate(R.layout.ID, null); 
AlertDialog.Builder ad = new AlertDialog.Builder(Activity); 
ad.setview(v);