You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

秒表APP开发:如何实现随手机横竖屏切换的双Activity布局

实现秒表应用横屏双界面相邻显示的方案

嗨,刚好有类似的实践经验,给你一套清晰的解决方案——既能保留竖屏时两个独立界面的逻辑,又能完美实现横屏时两者相邻显示的需求:

核心思路:把两个Activity的内容重构为Fragment

原来各自独立的计时器界面和单圈记录界面,拆成两个独立的Fragment,这样就能灵活适配横竖屏的布局需求:竖屏时分开成两个Activity展示,横屏时嵌入同一个布局并排显示。


步骤1:拆分界面为Fragment

  • 计时器控制Fragment(比如命名为StopwatchControlFragment):包含启动/暂停、重置、记录单圈、跳转单圈列表的按钮,完整保留原来的计时器核心逻辑。
  • 单圈记录Fragment(比如命名为LapRecordsFragment):包含显示单圈时间的ScrollView和返回按钮,负责单圈数据的展示逻辑。

步骤2:创建横竖屏专属布局文件

Android会根据屏幕方向自动加载对应布局,我们只需要准备两套布局:

  1. 竖屏布局(res/layout/activity_main.xml):
    只加载StopwatchControlFragment,保持原有独立界面的逻辑。用户点击跳转按钮时,启动新Activity展示LapRecordsFragment:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- 默认显示计时器控制Fragment -->
        <fragment
            android:name="com.yourpackage.StopwatchControlFragment"
            android:id="@+id/stopwatch_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    
  2. 横屏布局(res/layout-land/activity_main.xml):
    使用LinearLayout(或ConstraintLayout)将两个Fragment并排摆放,实现相邻显示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <!-- 左侧:计时器控制Fragment -->
        <fragment
            android:name="com.yourpackage.StopwatchControlFragment"
            android:id="@+id/stopwatch_fragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <!-- 右侧:单圈记录Fragment -->
        <fragment
            android:name="com.yourpackage.LapRecordsFragment"
            android:id="@+id/lap_records_fragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
    

步骤3:适配跳转与返回逻辑

  • 跳转按钮逻辑(在StopwatchControlFragment中):
    判断当前屏幕方向,横屏时直接控制单圈Fragment的可见性,竖屏时启动新Activity:

    buttonJumpToLaps.setOnClickListener(v -> {
        int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏模式:显示单圈Fragment(如果默认隐藏的话)
            LapRecordsFragment lapFragment = (LapRecordsFragment) getParentFragmentManager().findFragmentById(R.id.lap_records_fragment);
            if (lapFragment != null) {
                lapFragment.getView().setVisibility(View.VISIBLE);
            }
        } else {
            // 竖屏模式:启动新Activity展示单圈列表
            Intent intent = new Intent(getActivity(), LapRecordsActivity.class);
            startActivity(intent);
        }
    });
    
  • 返回按钮逻辑(在LapRecordsFragment中):
    同样判断屏幕方向,横屏时隐藏自身,竖屏时关闭当前Activity:

    buttonBack.setOnClickListener(v -> {
        int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏模式:隐藏单圈Fragment
            getView().setVisibility(View.GONE);
        } else {
            // 竖屏模式:关闭当前Activity
            getActivity().finish();
        }
    });
    

额外优化建议

  • 用ViewModel共享计时器和单圈数据,避免Fragment之间繁琐的数据传递,保证横竖屏切换时数据不丢失、状态一致。
  • 横屏模式下可以默认直接显示两个Fragment,省去用户点击跳转的步骤,具体可根据你的产品需求调整。

内容的提问来源于stack exchange,提问作者John Doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:25:18