2011-01-31 116 views
4

我如何添加标题到这个自定义对话框?如何将标题添加到自定义对话框?

enter image description here

我已经试过这样

public void customDialog() 
{ 
    Dialog dialog=new Dialog(this); 
    dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.string.app_name); 
    dialog.setContentView(R.layout.dialog_submit); 
    TextView edit_model=(TextView) dialog.findViewById(R.id.edit_model); 
    edit_model.setText(android.os.Build.DEVICE); 
    dialog.show(); 
}//end of custom dialog function 

我试图设置标题这样太.. dialog.setTitle("Enter Details");但这也并没有取得任何结果。那么我如何设置标题到这个自定义对话框?

这是我用于定制对话框的dialog_submit.xml文件。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/layout_root" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dp" 
      > 
    <TextView android:id="@+id/txt_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFF" 
      android:text="Name" 
      android:textStyle="bold" 
      /> 
    <EditText android:id="@+id/edit_name" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/txt_name" 
      /> 
<TextView android:id="@+id/txt_model" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFF" 
      android:layout_below="@+id/edit_name" 
      android:text="Phone Model" 
      /> 
<TextView android:id="@+id/edit_model" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/txt_model" 
      /> 

<Button android:id="@+id/but_cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/edit_model" 
      android:text="Cancel"  
      /> 
<Button android:id="@+id/but_submit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/edit_model" 
      android:layout_toRightOf="@+id/but_cancel"  
      android:text="Submit"  
      />      
</RelativeLayout> 

回答

3

你试过了吗?

dialog.setTitle(R.string.app_name); 
+0

我已经在上面提到它,dialog.setTitle(“???”)不是任何结果。 – Shijilal 2011-01-31 15:32:15

+0

@Shijilal:再看看Cristian的答案 - 他用int而不是CharSequence来设置标题。确保你也尝试了这种方法。它可能无法正常工作,但请确保您尝试了两个setTitle()选项。 – McStretch 2011-01-31 15:40:08

+0

@McStretch我试过..但它不工作.dialog.SetTitle将工作,如果我删除dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);如果我删除这个,那么标题和第一个视图(ie.name)之间有很大差距,这就是为什么我想要与FEATURE.CUSTOM_TITLE – Shijilal 2011-01-31 15:50:30

3

使用您的布局定义,这一段代码:

public void customDialog() 
{ 
    Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.dialog_submit); 
    TextView edit_model = (TextView) dialog.findViewById(R.id.edit_model); 
    edit_model.setText(android.os.Build.DEVICE); 
    dialog.setTitle("Enter Details"); 
    dialog.show(); 
} 


我得到这个对话框:

enter image description here


因此,您可能想再次尝试使用dialog.setTitle(“输入详细信息”)。
我使用了运行Android 2.1的模拟器。

0

如果您仍然需要回答此问题,您可能会尝试的是AlertDialog.Builder对象。在这个对象中,你也可以调用setMessage("Title")方法,它将在你最终创建的Dialog中设置标题。此外,您还可以指定一个positiveButton,neutralButton和一个negativeButton(让我们按照“添加”,“确定”和“取消”顺序,尽管您可以指定自己的文本)。

我相信问题在于当你打电话给Dialog dialog = new Dialog(this)onCreateDialog(int id)被调用。但是这里有一个问题:这个方法被调用一次并且提供一个Dialog,当你需要一个新的Dialog时它会被重用。但是,Dialog不能再编辑(据我所知)。那么可能与onPrepareDialog(int id, Dialog dialog)方法,但我仍然试图让自己工作。我的观点是,在创建之后,不能再编辑对话框上的用户界面。因此,工作的方式是在代码中覆盖onCreateDialog(int id),创建AlertDialog.Builder(或任何基于Dialog的文件),并在此设置标题,布局和按钮。在此之后,您可以拨打create()方法,该方法实际上会用您的设置构建Dialog

@Override 
public dialog onCreateDialog(int id){ 
    // Create the View to use in the Dialog. 
    LayoutInflater inflater = getLayoutInflater(); 
    // Inflate the View you want to set as the layout. 
    final View layout = inflater.inflate(R.layout.your_dialog_layout, 
             (ViewGroup) findViewById(R.id.your_parent_view); 
    // Create Dialog Builder Object to create Dialog from. 
    AlertDialog.Builder adBuilder = new AlertDialog.Builder(this); 
    // Set the title to use. 
    adBuilder.setMessage("Your title"); 
    // Add only a positive button. 
    adBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener(){ 
     @Override 
     public void onClick(DialogInterface dialog, int which){ 
      // Handle click on positive button here. 
     } 
    }; 
    // Set the layout which you want to use (inflated at the beginning). 
    adBuilder.setLayout(layout); 
    // After you've set all the options you want to set, call this method. 
    AlertDialog dialog = adBuilder.create(); 
    return dialog; 
} 

这将创建一个标题为“你的标题”,即用您指定的布局,与文本“添加”一个按钮Dialog。请注意,正面,中性和负面按钮的主要区别在于Dialog上的布局相应更改(正面=左面,中性=中等和负面=右面)。

欲了解更多信息,我会建议你看到有关这方面的文档。

16

使用你的一些片断:

public void customDialog() { 
    Dialog dialog=new Dialog(this); 
    dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    dialog.setContentView(R.layout.dialog_submit); 
    dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); 
    dialog.show(); 
} 

RES /布局/ custom_title.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="This is a custom title"/> 
0

如果你有3个或更少的按钮,为什么不使用的AlertDialog

我的AlertDialog看起来是这样的:

enter image description here

我的Java代码:

LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.add_filter, null); 

AlertDialog alertDialog = new AlertDialog.Builder(this) 
     .create(); 
alertDialog.setTitle("AlertDialog title"); 
alertDialog.setMessage("AlertDialog message"); 
alertDialog.setView(view); 
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
        int which) { 
       dialog.dismiss(); 
      } 
     }); 
alertDialog.show(); 

我的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="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <Spinner 
     android:id="@+id/spinner_filter" 
     android:layout_width="wrap_content" 
     android:spinnerMode="dropdown" 
     android:layout_height="wrap_content" 
     android:entries="@array/filter_array" 
     android:prompt="@string/filter_prompt" /> 

</LinearLayout> 

简单,但是做了你所需要的。

0

这个问题是旧的,但我的解决方案是使用主相对布局内的相对布局。这样你可以创建自己的标题。如果以这种方式使用它,它不会将顶级TextView看作标题:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="10dp" 
     > 
<RelativeLayout 
     android:id="@+id/layout_root" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
<TextView android:id="@+id/txt_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#FFF" 
     android:text="Name" 
     android:textStyle="bold" 
     /> 
<EditText android:id="@+id/edit_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txt_name" 
     /> 
<TextView android:id="@+id/txt_model" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#FFF" 
     android:layout_below="@+id/edit_name" 
     android:text="Phone Model" 
     /> 
<TextView android:id="@+id/edit_model" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txt_model" 
     /> 

<Button android:id="@+id/but_cancel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/edit_model" 
     android:text="Cancel"  
     /> 
<Button android:id="@+id/but_submit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/edit_model" 
     android:layout_toRightOf="@+id/but_cancel"  
     android:text="Submit"  
     /> 
</RelativeLayout>      
</RelativeLayout> 

这似乎是最简单的方法。

0

请使用此行来隐藏内置的标题从对话框

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

并在您的布局文件中添加textView。

相关问题