簡體   English   中英

相應地更改為風景/人像模式

[英]To change accordingly to landscape/portrait mode

我有一個日期/時間選擇器,我希望他們根據其布局進行反應。

考慮橫向模式:

在此處輸入圖片說明

這是我想要的理想位置。 但是,如果將其切換為人像模式,則會得到如下所示的內容:一部分時間丟失了。

在此處輸入圖片說明

我當前的代碼是:

<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可以實現我想要的縱向模式,但是在橫向模式下會弄亂,反之亦然。

有什么替代方法嗎? 每當屏幕模式切換時,是否可以通過編程方式更改布局的方向?

當屏幕方向改變而不是活動時,再次這樣做:

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

編碼愉快!!

您可以創建一個包含屏幕橫向布局的layout-land目錄。 在這種情況下,您可以將原始布局放置在layout-land目錄中,然后將orientation:vertical布局放置在布局文件夾中。

Android文檔包含有關如何支持不同屏幕的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM