其他分享
首页 > 其他分享> > android – 如何在时间选择器上设置自定义标题?

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

作者:互联网

我正在制作一个带标题的日期选择器,但是它的视图很难看,因为没有底部边缘,我怎么能设置一个余地让它变得不那么漂亮?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;
}

解决方法:

试试这个:

在代码中执行此操作:

        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

标签:android,timepickerdialog
来源: https://codeday.me/bug/20190609/1203600.html