0

我是 Android 开发的新手。我正在尝试使用方向更改在两个活动之间切换。我的想法是使用三个活动一个 TabActivity 和一个正常的活动。这是一些伪代码:

public class Main extends Activity{
// this is the entry point into my app
    @Override
    public void onCreate(...){
        if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            // Start Activity1
    } else { //Start Activity2}
}

Activity Activity1 和 Activity2 将覆盖其 onPause() 函数,以再次启动 Main Activity。

onPause(){
    Intent intent = new Intent(this.ActivityX, Main.class);
    startActivity(intent);
}

\/\/\/\/ 编辑 \/\/\/\/

好的,我正在采取不同的方法。因为我需要一个 TabActivity,因为我希望能够在我的选项卡中启动不同的活动(我认为在这种情况下我必须使用 TabActivity)。

我已经编辑了 Manifest.xml 并为 TabActivity 和第二个 Activity 添加了 android.configChanges="orientation"。然后我重写了 onConfigurationChanged() 函数。使用这种方法,我可以从横向(这是“正常”活动”)切换到纵向(TabActivity)。相反的方式不起作用,我还不知道为什么。我完全一样onConfigurationChanged 函数(复制和粘贴)并且只更改了必要的部分。

覆盖在选项卡中启动的活动的 onConfigurationChanged 也没有效果。

4

2 回答 2

2

您不必编写任何代码 - Android 已经自动处理了这个。只需创建两个不同的布局资源文件夹:

/res/layout-land  // layout resources for landscape
/res/layout-port  // portrait layout

xyz.xml将同名但内容不同的资源描述文件放入此文件夹。一种用于纵向,另一种用于横向。

请注意,您可以对可绘制对象(位图)或任何其他资源(文本)使用相同的技术(-port&-land限定符)。

注意: Android 1.5 不支持此功能。如果要支持此版本,则必须另外添加/res/layout文件夹。

于 2010-11-12T18:03:43.423 回答
0

如果您仍然想在纵向模式下使用选项卡时使用 TabActivity 的优势,而在横向模式下没有选项卡,您可以使用以下(丑陋但仍然有效)解决方法。创建 /res/layout-land 文件夹并放置一个相应的布局文件(它应该与布局文件夹中的文件具有相同的名称,用于纵向)。但是,此文件应包含 TabActivity 工作所需的块。没关系,添加这些块并将它们的可见性设置为“已消失”,如以下代码段所示:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"
        android:visibility="gone" />
    <!-- Feel free to add your REAL layout for the landscape -->
</LinearLayout>
</TabHost>

当然,请确保您在活动课程中正确处理内容管理,具体取决于方向。

于 2011-06-07T10:31:48.547 回答