(请注意,我在后面添加了一个更好的答案,越过这条线。我将把这个放在首位,以防它更适合其他人的需求。)
是的,我一直在处理同样的问题。我有一个有点笨拙的解决方案。我注意到您可以使用 setRotate 旋转几乎任何视图。不幸的是,这只会旋转它的图像或其他东西,并不能完全处理问题或位置、大小或对齐方式。即便如此,我还是设法拼凑出一些可以弥补它的东西,尽管它只完成了一半(如果你旋转到纵向,它就不能很好地工作)。每当旋转平板电脑或重新排列布局或其他情况时,都会调用以下内容。
private LinearLayout llMain;
private LinearLayout ll1;
private LinearLayout mainView;
private TabHost myTabHost;
public void setStuff() {
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (display.getOrientation() == 0 || display.getOrientation() == 2) {
ViewGroup.LayoutParams lp = myTabHost.getLayoutParams();
int top = 0;
int left = 0;
int height = 696;
int width = 1280;
int centerx = (width / 2) + left;
int centery = (height / 2) + top;
lp.height = width;
lp.width = height;
myTabHost.setLayoutParams(lp);
myTabHost.setTranslationX(centerx - (height / 2));
myTabHost.setTranslationY(centery - (width / 2));
lp = mainView.getLayoutParams();
lp.width = 1280;
lp.height = 696;
mainView.setLayoutParams(lp);
lp = llMain.getLayoutParams();
lp.width = 1280;
lp.height = 696;
llMain.setLayoutParams(lp);
ll1.setRotation(-90);
lp = ll1.getLayoutParams();
lp.height = 696;
lp.width = 1141;
ll1.setLayoutParams(lp);
left = 0;
top = 0;
height = 696;
width = 1141;
centerx = (width / 2) + left;
centery = (height / 2) + top;
ll1.setTranslationX(centery - (width / 2));
ll1.setTranslationY(centerx - (height / 2));
//ll1.setTranslationX(tx);
//ll1.setTranslationY(ty);
} else {
ViewGroup.LayoutParams lp = myTabHost.getLayoutParams();
int top = 0;
int left = 0;
int height = 1280;
int width = 696;
int centerx = (width / 2) + left;
int centery = (height / 2) + top;
lp.height = width;
lp.width = height;
myTabHost.setLayoutParams(lp);
myTabHost.setTranslationX(centerx - (height / 2));
myTabHost.setTranslationY(centery - (width / 2));
lp = mainView.getLayoutParams();
lp.width = 696;
lp.height = 1280;
mainView.setLayoutParams(lp);
lp = llMain.getLayoutParams();
lp.width = 696;
lp.height = 1280;
llMain.setLayoutParams(lp);
ll1.setRotation(-90);
lp = ll1.getLayoutParams();
lp.height = 1141;
lp.width = 696;
ll1.setLayoutParams(lp);
left = 0;
top = 0;
height = 1141;
width = 696;
centerx = (width / 2) + left;
centery = (height / 2) + top;
ll1.setTranslationX(centery - (width / 1));
ll1.setTranslationY(centerx - (height / 1));
//ll1.setTranslationX(tx);
//ll1.setTranslationY(ty);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setStuff();
//setContentView(R.layout.main);
}
llMain 包含 mainView 包含 myTabHost 包含 ll1;您需要在初始化代码中这样分配它们。这个想法是 TabHost 顺时针旋转,并且它的尺寸必须手动交换,以便它的容器现在知道它是不同的大小/形状,否则边缘会被切断。此外,它还进行了一些计算,以便它围绕正确的点旋转,或者至少在正确的位置结束。结果并不总是有意义,所以我只是改变了一些东西直到它起作用,至少在横向模式下。TabHost 的内容也被旋转,因此其中有代码将其旋转回来,至少是第一个选项卡,即 ll1。其他选项卡也需要类似地向后旋转。我搞砸了一段时间,可能有些事情是不必要的。例如,主视图。我认为不需要在那里。哦,如果 TabHost 位于不同的位置,则需要以某种方式调整数字。祝你好运。无论如何,我希望这对某人有所帮助。伙计,Android GUI 太令人恼火了……我希望他们能修复它……不过,我承认,尽管有所有的障碍和挫折,任何组件的旋转很酷。我只是希望其他一切都像你期望的那样工作。
嘿,我刚刚想出了如何让它正确地做到这一点!标签保持水平,因此这可能是也可能不是一件好事,这取决于您想要什么,但无论如何:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">
<FrameLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" android:id="@android:id/tabcontent"></FrameLayout>
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:id="@android:id/tabs"></TabWidget>
</ScrollView>
</LinearLayout>
</TabHost>
</LinearLayout>
此 XML 文件定义了一个空的 tabhost,右侧有选项卡。只需在代码中添加选项卡;周围有帖子。复制/更改/做任何你需要做的事情来满足你的需要。另外,因为它很酷,所以我在 tabwidget 周围添加了一个滚动视图,因此如果您有太多选项卡,它会自动允许滚动。如果您不想要它,请摆脱它。滚动内容主要来自http://java.dzone.com/articles/scrolling-tabs-android。耶!