我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/menus_layer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/orientation_layer"
android:visibility="gone">
<include layout="@layout/layer_orientation"/>
</FrameLayout>
<Button
android:id="@+id/interruption_menu"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Interruption"/>
<Button
android:id="@+id/action_menu"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action"/>
</RelativeLayout>
和layer_orientation.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/check_orientation"
android:gravity="center"
android:layout_gravity="center"
android:visibility="visible"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="5dp"
android:background="@android:color/holo_red_light"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@android:drawable/ic_menu_always_landscape_portrait"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/please_check_orientation"
android:layout_gravity="fill" />
</LinearLayout>
保存布局的活动的方向设置为横向。使用OrientationEventListener我想显示并相应地旋转orientation_layer。类似的东西:
private final static int THRESHOLD = 45;
private final static int NORMAL = 270;
mOrientationEventListener = new OrientationEventListener(mContext, SensorManager.SENSOR_DELAY_UI) {
@Override
public void onOrientationChanged(int orientation) {
if (mCallback != null) {
if (orientation <= NORMAL + THRESHOLD && orientation >= NORMAL - THRESHOLD)
mCallback.hideOrientationLayer();
else {
int rot = (NORMAL - orientation - THRESHOLD) / 90;
if (rot >= 0)
rot = (rot + 1) * 90;
else
rot = rot * 90;
mCallback.showOrientationLayer(rot);
}
}
}
};
@Override
public void showOrientationLayer(int rotation) {
mOrientationLayer.setRotation(rotation);
mOrientationLayer.setVisibility(View.VISIBLE);
}
@Override
public void hideOrientationLayer() {
mOrientationLayer.setVisibility(View.GONE);
}
如果我将设备向右旋转,则框架布局显示正常,但如果我向左旋转,则只显示一半布局,另一半被切断。以下是一些屏幕截图:https://www.dropbox.com/sh/ypu6txrwpve27ud/Lv_9lTJybA 还有一个Android Studio测试项目:https://www.dropbox.com/sh/f5kiaw7y5ueb0zk/FASOGh54SD
经过一些测试后,我得出结论,这仅在KitKat上出现。
有没有人对可能出现的问题有所了解?提前谢谢!