根据电话方向,2个活动彼此相邻显示

时间:2018-02-21 19:50:25

标签: android android-layout android-orientation

我正在尝试创建一个具有Buttons的秒表应用,允许用户启动 / 暂停计时器,重置计时器,记录一圈时间。我还会有一个Button点击后会在ScrollView中显示包含单圈时间的新视图。

该屏幕会有一个Button,这会将用户带回计时器

仅此一项就相对简单了,它只是两个独立的Activities。但是,当手机切换到横向模式时,我希望两个屏幕(计时器单圈时间)彼此相邻显示。

有一种简单的方法吗?

我在考虑只有一个Activity内有两个LinearLayouts(一个用于秒表屏幕,另一个用于一圈时间)并且通常在Button之间切换的Activities只是更改其中一个视图的透明度,因此只有一个可见?

我想有一种更简单的方法可以做到这一点,我不确定这个解决方案是否允许我并排显示每个屏幕。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我不知道有什么方法可以同时展示两项活动,但我可以告诉你我将如何解决这个问题:

我首先要在他们自己的专用布局文件中创建秒表布局和单圈时间布局 - 所以在这个例子中,我们有 / res / layout / layout_stop_watch

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- stopwatch views here -->

</RelativeLayout>

...和 / res / layout / layout_lap_times

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- lap times views here -->

</RelativeLayout>

然后在 /res/layout/activity_main.xml 中,我们在纵向模式下有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper
    android:id="@+id/root_viewFlipper"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- stopwatch views included in activity_lap_times layout -->
    <include layout="@layout/layout_stop_watch" />

    <!-- lap time views included in activity_lap_times layout -->
    <include layout="@layout/layout_lap_times" />

</ViewFlipper>

ViewFlipper就在那里,所以当用户点击按钮查看单圈时间时,我们可以拨打ViewFlipper.setDisplayedChild(1)来显示单圈时间布局。只需致电ViewFlipper.setDisplayedChild(0)即可返回秒表布局。

对于横向模式,我们有一个单独的布局 - /res/layout-land/activity_main.xml 。此布局将自动以横向模式显示,因此我们不需要以编程方式处理该布局。我们可以这样设置:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/layout_stopwatch"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <!-- stopwatch views included in activity_lap_times layout -->
        <include layout="@layout/layout_stop_watch" />

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/layout_lap_times"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <!-- lap time views included in activity_lap_times layout -->
        <include layout="@layout/layout_lap_times" />

    </FrameLayout>

</LinearLayout>

就是这样。

要记住的一件事是ViewFlipper不能在横向模式中使用,所以在设置显示的子项之前,您要检查它是否为null。另请注意,从纵向切换到横向将重新创建您的活动。如果要保存任何视图状态,可以使用savedInstanceState

这样做的好处是,我们不必为秒表和单圈时间布局编写代码两次,用于纵向和横向。此外,由于我们对所有活动使用了一个活动,因此所有相关的java代码都可以驻留在 MainActivity.java 中。