横向模式下的TimePickerDialog小部件(PreferenceScreen) [英] TimePickerDialog widget in landscape mode (PreferenceScreen)

查看:149
本文介绍了横向模式下的TimePickerDialog小部件(PreferenceScreen)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

横向模式下PreferenceScreen中的TimePickerDialog小部件太小. 屏幕截图

The TimePickerDialog widget in PreferenceScreen in landscape mode is too small. screenshot

我已经按照以下说明尝试了两种方法(PreferenceActivity和PreferenceFragments): PreferenceScreen

I've tried out both approaches (PreferenceActivity and PreferenceFragments) according to these instructions: TimePicker in PreferenceScreen

使用时间选择器标题时,在横向模式下,TimePickerDialog布局为空.因此,我已禁用标题.在PreferenceActivity版本中,我已将setDialogTitle("");添加到TimePreference.在PreferenceFragmentCompat版本中,我向TimePreference添加了以下代码:

The TimePickerDialog layout is empty in landscape mode, when using a timepicker title. So I've disabled the title. In the PreferenceActivity Version I've added setDialogTitle(""); to TimePreference. In the PreferenceFragmentCompat Version I've added the following code to TimePreference:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.setTitle("");
    return dialog;
}

在两个版本中,时钟都是横向显示的,但是小部件太小; (当使用在PreferenceScreen之外没有标题的timepicker对话框时,对于小部件来说,我没有任何问题)

In both versions, the clock is presented in landscape, but the widget is too small; (I have no problems with too small widgets, when using timepickerdialogs without titles outside of the PreferenceScreen)

有什么想法如何在PreferenceScreen中实现TimePicker,以便在横向模式下可以正常布局吗?

Any ideas how to implement TimePicker in the PreferenceScreen, so that the layout is ok in landscape mode?

推荐答案

在摩托罗拉Moto G5 Plus手机(Android 7)上,在横向模式下也存在相同的问题.肖像还可以.从日志输出看来,包括时间选择器的首选项alertdialog垂直安装在屏幕上,因此时间选择器小部件的对话框内垂直空间太小.可能的解决方案:

Same problem here on a Motorola Moto G5 Plus phone, Android 7, in landscape mode. Portrait is ok. From the log output it looks like the preference alertdialog including the timepicker is fit vertically on the screen, thus leaving too little vertical space inside the dialog for the timepicker widget. Possible solutions:

  • 如果对话框中仅缺少dp的垂直空间,请将<TimePicker>包裹在<ScrollView>中.

使用XML android:scaleXandroid:scaleY属性缩小TimePicker(

Scaling the TimePicker down using the XML android:scaleX and android:scaleY properties (How to make the TimePicker smaller). I am currently testing this.

将应用设置为全屏/沉浸式可能会有所帮助,因为这会增加可用高度".

Setting the app full screen / immersive might help because it increases the 'available height'.

将TimePicker小部件设置为旋转"模式,android:timePickerMode="spinner"

Setting the TimePicker widget to 'spinner' mode, android:timePickerMode="spinner"

scaleX,scaleY解决方案可以使用,但是TimePicker看起来很丑.将应用程序设置为全屏模式会删除屏幕上的UI控件,对于许多应用程序来说,这可能不是最佳选择.在滚动视图中包装时间选择器也不是最佳选择.

The scaleX, scaleY solution is functional, but the TimePicker looks ugly. Setting the app as full screen removes on screen UI controls, which may not be the best choice for many apps. Wrapping the timepicker in a scrollview is not optimal either.

我制作了两个XML TimePicker布局,一个使用android:timePickerMode="spinner",另一个使用"clock".使用资源限定符(文件夹名称中的后缀)为可用的屏幕高度:"h360dp"选择时钟布局.因此,只有当设备的当前屏幕可用高度(不包括屏幕上的UI控件)在当前方向上大于360dp时,才会显示时钟.也不是最佳选择,但它可以工作.

I made two XML TimePicker layouts, one with android:timePickerMode="spinner", the other with "clock". The clock layout is selected using the resource qualifier (suffix in the folder name) for available screen height: "h360dp". So that the clock is only showing when the device's available screen height, in the current orientation, excluding on-screen UI controls, is greater than 360dp. Not optimal either, but it works.

修改2: 下面介绍的方法更好.经过测试,可以在Android 4之前的实际设备上运行,直到7.TimePicker对话框可以横向正确显示.

Edit 2: The approach described below is better. Tested and works on real devices from Android 4 until 7. The TimePicker dialog is properly showing in landscape orientation.

步骤是:

  • Include a general Preference item in the preferences xml file
  • Set a click listener on this Preference using onPreferenceTreeClick()
  • When this Preference is clicked, show a regular (not compatibility library) TimePickerDialog like described in the 'Pickers' guide https://developer.android.com/guide/topics/ui/controls/pickers.html
  • Save the time set on the TimePicker manually in the SharedPreferences

我将此处涉及的所有代码文件的示例代码放在Stackoverflow上:具有自定义本机(不兼容)对话框首选项的应用程序兼容性,其中包含TimePicker

I put example code of all code files involved here on Stackoverflow: Appcompatactivity with custom native (not compatibility) dialogpreference containing a TimePicker

这篇关于横向模式下的TimePickerDialog小部件(PreferenceScreen)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆