相应地更改为风景/人像模式 [英] To change accordingly to landscape/portrait mode
问题描述
我有一个日期/时间选择器,我希望他们根据其布局进行反应.
I have a date/time picker which I want them to react according based on its layout.
考虑使用风景模式:
这是我想要的理想位置.但是,如果将其切换为人像模式,则会得到如下所示的内容:一部分时间丢失了.
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.
我当前的代码是:
<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可以实现我想要的纵向模式,但是在横向模式下会弄乱,反之亦然.
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.
有什么替代方法?每当屏幕模式切换时,是否可以通过编程方式更改布局的方向?
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
}
编码愉快!
这篇关于相应地更改为风景/人像模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!