好,我知道了!我的下午有这个好主意:
5.在布局xml复制粘贴整个UI并使用原来的纵向和新的横向:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/preview_frame_overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:orientation="vertical"
android:visibility="visible" >
<!-- here is the rest of the portait layout code -->
</LinearLayout>
<LinearLayout
android:id="@+id/preview_frame_overlay2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:orientation="vertical"
android:rotation="90"
android:visibility="gone" >
<!-- here is the rest of the landscape layout code -->
</LinearLayout>
当然你应该使用android:configChanges="keyboardHidden|orientation|screenSize"
在您的扫描活动中,您应该根据方向更改可见和可用的按钮:
//in onCreate
if (!ProductionActivity.settingLandscape) // if landscape set to portait
{
portraitView.setVisibility(View.VISIBLE);
landscapeView.setVisibility(View.GONE);
findViewById(R.id.view_finder).setVisibility(View.VISIBLE);
findViewById(R.id.view_finder2).setVisibility(View.GONE);
ChangeToLandscape(false);
} else
{
portraitView.setVisibility(View.GONE);
landscapeView.setVisibility(View.VISIBLE);
findViewById(R.id.view_finder).setVisibility(View.GONE);
findViewById(R.id.view_finder2).setVisibility(View.VISIBLE);
ChangeToLandscape(true);
android.view.ViewGroup.LayoutParams params1 = landscapeView.getLayoutParams(); //used to resize screen
params1.height = mDisplay.getWidth();
landscapeView.setLayoutParams(params1);
}
以及ChangeToLandscape
我们更新按钮引用和委托的方法:
private void ChangeToLandscape(boolean landscape)
{
if (landscape)
{
hintTextView = (TextView) findViewById(R.id.hint_text2);
foundTextView = (TextView) findViewById(R.id.num_found_text2);
tvLastBarcode = (TextView) findViewById(R.id.tv_lastBarcode2);
doneButton = (Button) findViewById(R.id.button_done2);
bMultiscan = (Button) findViewById(R.id.bMultiscan2);
toggleTorchButton = (Button) findViewById(R.id.button_toggle_torch2);
toggleLineButton = (Button) findViewById(R.id.button_toggle_line2);
bRotate = (Button) findViewById(R.id.bRotate2);
viewfinderView = findViewById(R.id.view_finder2);
} else
{
hintTextView = (TextView) findViewById(R.id.hint_text);
foundTextView = (TextView) findViewById(R.id.num_found_text);
tvLastBarcode = (TextView) findViewById(R.id.tv_lastBarcode);
doneButton = (Button) findViewById(R.id.button_done);
bMultiscan = (Button) findViewById(R.id.bMultiscan);
toggleTorchButton = (Button) findViewById(R.id.button_toggle_torch);
toggleLineButton = (Button) findViewById(R.id.button_toggle_line);
bRotate = (Button) findViewById(R.id.bRotate);
viewfinderView = findViewById(R.id.view_finder);
}
//and then we re-assign the buttons delegates
doneButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
doneScanning();
}
}); //....etc
}
使用这个想法,活动永远不会重新启动,因此我的数据被保留并且效果很好。