How to add title to the custom Dialog?

2019-02-17 14:08发布

How can i add title to this custom dialog??

enter image description here

I have tried like this

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

I have tried to set title like this too..dialog.setTitle("Enter Details"); but this too didn't yielded any result. So how can i set title to this custom dialog??

This is my dialog_submit.xml file used for the custom dialog.

 <?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>

7条回答
干净又极端
2楼-- · 2019-02-17 14:43

Why not use an AlertDialog if you have 3 or less buttons?

My AlertDialog looks like this:

enter image description here

My java code:

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();

My 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>

Simple yet does what you need.

查看更多
Bombasti
3楼-- · 2019-02-17 14:53

This question is old, but my solution is using an relative layout within the main relative layout. That way you can create your own title. It doesn't see a top TextView as a title if you use it that way:

  <?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>

This seems to be the easiest way.

查看更多
迷人小祖宗
4楼-- · 2019-02-17 14:58

Using some of your snippet:

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/layout/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"/>
查看更多
乱世女痞
5楼-- · 2019-02-17 15:00

Please use this line in to hide built-in title from dialog

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

and add textView in your layout file.

查看更多
爷的心禁止访问
6楼-- · 2019-02-17 15:05

Using your layout definition and this piece of code:

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( );
}


I get this dialog:

enter image description here


So, you might want to try dialog.setTitle( "Enter Details" ) again.
I used the emulator running Android 2.1.

查看更多
We Are One
7楼-- · 2019-02-17 15:06

Did you try?

dialog.setTitle(R.string.app_name);
查看更多
登录 后发表回答