如何在android中设置DatePickerDialog的标题布局

时间:2017-05-07 14:45:16

标签: android datepickerdialog

我在android中创建一个DatePickerDialog,我希望我的datepickerdialog看起来像图像中的左侧对话框。但是当我编写自己的代码时,它只是水平文本日期(右边的对话框) 我该怎么办? My image

package com.example.luc.myapplication;
import android.app.DatePickerDialog;
import android.app.DialogFragment;

import android.os.Bundle;
import android.app.Fragment; 
import android.view.ViewGroup; 
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.DatePicker;
import android.app.Dialog; 
import java.util.Calendar;

/**
 * A simple {@link Fragment} subclass.
 */
public class DatePickerFragment extends DialogFragment implements 
DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    //Use the current date as the default date in the date picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog =  new DatePickerDialog(getActivity(), this, year, month, day);
    return dialog;
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    //Do something with the date chosen by the user
  LinearLayout layout = new LinearLayout(getActivity());
    layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    layout.setOrientation(LinearLayout.VERTICAL);

    view.setLayoutParams(layout.getLayoutParams());
    TextView tv = (TextView) getActivity().findViewById(R.id.tv);
    tv.setText("Date changed...");
    tv.setText(tv.getText() + "\nYear: " + year);
    tv.setText(tv.getText() + "\nMonth: " + month);
    tv.setText(tv.getText() + "\nDay of Month: " + day);

    String stringOfDate = day + "/" + month + "/" + year;
    tv.setText(tv.getText() + "\n\nFormatted date: " + stringOfDate);
}
}

单击MainActivity中的按钮

时调用对话框

0 个答案:

没有答案