android - 相应地更改为横向/纵向模式

标签 android android-layout

我有一个日期/时间选择器,我希望它们根据其布局使用react。

考虑横向模式:

enter image description here

这是我想要的完美位置。但是,如果我将其切换为纵向模式,则会出现如下内容:部分时间丢失。

enter image description here

我当前的代码是:

<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>

将内部 LinearLayout 的方向更改为 Vertical 将实现我想要的纵向模式,但它在横向模式下会搞砸,反之亦然。

有什么替代方法?是否可以在屏幕模式打开时以编程方式更改布局的方向?

最佳答案

当屏幕方向发生变化而不是再次重新创建 Activity 时,请执行以下操作:

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

编码愉快!!

关于android - 相应地更改为横向/纵向模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175600/

相关文章:

java - 足球数据 API JSON 解析错误

android - 应用程序不适用于 Android 4.1

java - Android 应用程序崩溃 - 我创建对象的位置就是原因

安卓双卡支持

android - 解析没有键的动态 Json 对象

android - 支持不同的屏幕尺寸 - xml 未正确加载。可能的错误?

android - Lint 警告Android

android - LinearLayout 内的 ListView 不会增长以显示所有子项

java - 如何缩放 Android TableLayout

Android 选项菜单在 Google map (Api v2) 上显示不佳