简体   繁体   中英

Android Time Picker shows in portrait and is cut off in landscape

I am trying to get the time picker to display the full clock in landscape. The TimePicker is in a GridLayout . It does not overlay the screen as a DialogFragment or a TimePickerDialog .

Currently, this is what it shows in landscape:

只有时间选择器显示

As you can see, there is nothing you can pick the time with, only the default time. I am trying to get it to show the clock as well just like in portrait (which works):

显示全职选择器

The layout of the time picker:

<package.name.ScrollableTimePicker
                android:id="@+id/time_picker"
                android:layout_height="wrap_content"
                android:layout_width="match_parent" />

The class that extends TimePicker :

public class ScrollableTimePicker extends TimePicker {

    public ScrollableTimePicker(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollableTimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollableTimePicker(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Keeps events to time picker
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
            ViewParent p = getParent();
            if (p != null) {
                p.requestDisallowInterceptTouchEvent(true);
            }
        }

        return false;
    }
}

I looked around the internet and found these posts that did not help me:

  1. Android TimePicker not displayed well on landscape mode

I do not have a title for my time picker.

  1. ScrollView fighting with scroll of TimePicker, Timepicker not scrolling as a result

It has already been implemented

  1. https://code.google.com/p/android/issues/detail?id=201766

Tried the code in manifest but still did not work.

UPDATE As siddesh has suggested, setting layout_height of the TimePicker to 200dp works but it cuts off the portrait. Now, the portrait is 200dp tall but is cut off and the landscape is shown fully:

完全显示高度为200dp的风景TimePicker

肖像TimePicker,高度为200dp,半截断。

I had this problem too when the TimePicker was in a ScrollView, your diagnosis was helpful.

You could set the height of the TimePicker programmatically in your activity.

In onCreate() add:

TimePicker tp = (TimePicker) findViewById(R.id.time_picker);

// get display dimensions
Point p = new Point();
getWindowManager().getDefaultDisplay().getSize(p);

int newHeight;

if(p.x > p.y) {
    // if landscape, convert 200dp to px
    newHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
} else {
    newHeight = WindowManager.LayoutParams.WRAP_CONTENT;
}

// set TimePicker height
tp.getLayoutParams().height = newHeight;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM