2011-01-31 117 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。

相關問題