如何在时间选择器上设置自定义标题?

时间:2015-08-12 22:50:26

标签: android timepickerdialog

我正在制作一个带标题的日期选择器,但视图很难看,因为没有边距底部,我怎么能留出余地让它变得不那么漂亮?timepickerdialog screenshot

你可以看到它非常难看。

我的代码:

if(endTime.isEmpty() || startTime.isEmpty()){

    int CustomHour       = 12;
    int CustomMinute     = 00;
    int hour             = CustomHour;
    int minute           = CustomMinute;

    TimePickerDialog tpd = new TimePickerDialog(ActivityAlarm.this, new TimePickerDialog.OnTimeSetListener() 
    {       

        int callCount = 0;   //To track number of calls to onTimeSet()              
        @Override
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) 
        {   

...

    }, hour, minute, DateFormat.is24HourFormat(ActivityAlarm.this));
    tpd.setTitle("Set Time");           
    // Create a new instance of TimePickerDialog and return it
    return  tpd;
}

1 个答案:

答案 0 :(得分:5)

试试这个:

在代码中执行此操作:

        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.text_layout, null);
        TextView texts=(TextView) dialogView.findViewById(R.id.textss);
        texts.setText("Start Time");
        tpd.setCustomTitle(dialogView);

在xml中创建一个名为:

的文件

<强> text_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">
<TextView
    android:id="@+id/textss"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

enter image description here