简体   繁体   English

相应地更改为风景/人像模式

[英]To change accordingly to landscape/portrait mode

I have a date/time picker which I want them to react according based on its layout. 我有一个日期/时间选择器,我希望他们根据其布局进行反应。

Consider on landscape mode: 考虑横向模式:

在此处输入图片说明

This is a perfect placement of what I want it to be. 这是我想要的理想位置。 However if I turn it over to portrait mode, I get something like below: part of the time is missing. 但是,如果将其切换为人像模式,则会得到如下所示的内容:一部分时间丢失了。

在此处输入图片说明

My current code is: 我当前的代码是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/DarkGrey" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <DatePicker
        android:id="@+id/custom_datePicker"
        android:calendarViewShown="false"
        android:layout_height="170dp"
        android:layout_width="wrap_content"
        android:layout_marginTop="-10dp"
        android:layout_marginBottom="-10dp" />

    <TimePicker
        android:id="@+id/custom_timePicker"
        android:layout_height="170dp"
        android:layout_width="wrap_content"
        android:layout_marginTop="-10dp"
        android:layout_marginBottom="-10dp" />

</LinearLayout>

<Button
    android:id="@+id/btnDone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Done"
    style="@style/Widget.AppCompat.Button.Colored"
    android:theme="@style/General.Button">
</Button>

</LinearLayout>

Changing the orientation of inner LinearLayout to Vertical would achieve what I want for portrait mode, but it messes up during landscape mode, vice versa. 将内部LinearLayout的方向更改为Vertical可以实现我想要的纵向模式,但是在横向模式下会弄乱,反之亦然。

What's an alternative way to this? 有什么替代方法吗? Is it possible to change the layout's orientation programmatically whenever the screen mode turns? 每当屏幕模式切换时,是否可以通过编程方式更改布局的方向?

When screen orientation change than activity again recreated so do this : 当屏幕方向改变而不是活动时,再次这样做:

int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
 //code for portrait mode
} else {
//code for landscape mode
}

Happy coding!! 编码愉快!!

You can create a layout-land directory that contains landscape layouts of your screens. 您可以创建一个包含屏幕横向布局的layout-land目录。 In this case you would place the original layout inside the layout-land directory and place the orientation:vertical layout inside your layout folder. 在这种情况下,您可以将原始布局放置在layout-land目录中,然后将orientation:vertical布局放置在布局文件夹中。

The Android documentation has information on how to support different screens Android文档包含有关如何支持不同屏幕的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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