Skip to content

Most visited

Recently visited

navigation

退出全屏 Activity

用户可以通过从左至右滑动退出 Android Wear Activity。如果应用有水平滚动(例如,包含地图的应用中提供的那种滚动),用户可通过导航到内容边缘,然后从左至右滑动退出。

按下电源按钮时用户将返回表盘。

滑动关闭手势

用户从左至右滑动可关闭当前屏幕。因此,建议您对下列项加以利用:

同时,建议不要让您的应用包含水平滑动手势。

Activity 的关闭

Activity 自动支持滑动关闭。从左至右滑动 Activity 会使 Activity 关闭,并且应用在返回栈中向下导航。

Fragment

您可以在 SwipeDismissFrameLayout 类中封装一个包含 Fragment 的视图,如下所示:

public class SwipeDismissFragment extends Fragment {
  private final Callback mCallback =
    new Callback() {
      @Override
        public void onSwipeStart() {
          // optional
        }

        @Override
        public void onSwipeCancelled() {
          // optional
        }

        @Override
        public void onDismissed(SwipeDismissFrameLayout layout) {
          // Code here for custom behavior such as going up the
          // back stack and destroying the fragment but staying in the app.
        }
      };

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    SwipeDismissFrameLayout swipeLayout = new SwipeDismissFrameLayout(getActivity());

    // If the fragment should fill the screen (optional), then in the layout file,
    // in the android.support.wear.widget.SwipeDismissFrameLayout element,
    // set the android:layout_width and android:layout_height attributes
    // to "match_parent".

    View inflatedView = inflater.inflate(R.layout.swipe_dismiss_frame_layout, swipeLayout, false);
    swipeLayout.addView(inflatedView);
    swipeLayout.addCallback(mCallback);

    return swipeLayout;
    }
}

:在穿戴式设备支持库中, SwipeDismissFrameLayout 类取代了一个与其类似且已弃用的类。

水平可滚动视图

在某些情况下,例如在包含的地图支持平移的视图里,界面不会阻止水平滑动。在此情境下,有两个选择:

要将视图封装在 SwipeDismissFrameLayout 对象中,请查看下列示例:

布局文件的 XML

<android.support.wear.widget.SwipeDismissFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe_dismiss_root" >

    <TextView
        android:id="@+id/test_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Swipe me to dismiss me." />
</android.support.wear.widget.SwipeDismissFrameLayout>

代码段

SwipeDismissFrameLayout testLayout =
    (SwipeDismissFrameLayout) activity.findViewById(R.id.swipe_dismiss_root);
testLayout.addCallback(new SwipeDismissFrameLayout.Callback() {
    @Override
    public void onDismissed(SwipeDismissFrameLayout layout) {
        layout.setVisibility(View.GONE);
    }
  }
);

通常不建议的做法:停用滑动关闭

不建议全面停用滑动关闭,因为用户期望通过滑动来关闭任何屏幕。在例外情况下,您可以扩展默认主题背景(在样式资源中)并将 android:windowSwipeToDismiss 属性设置为 false

<resources>
  <style name="AppTheme" parent="@android:style/Theme.DeviceDefault">
    <item name="android:windowSwipeToDismiss">false</item>
  </style>
</resources>

然后您就可以通知用户(在其首次使用您的应用时),他们可以通过按电源按钮退出应用。

Wear 2.0 之前的关闭行为

在 Wear 2.0 之前,DismissOverlayView 类用于实现供用户用来关闭应用的长按。不应再使用 DismissOverlayView 类。

此外,不再建议通过长按来关闭应用。请勿实现通过长按关闭全屏 Activity(例如 Google 地图等平移或沉浸式 Activity)。

通过电源按钮关闭

按电源(硬件)按钮会发送电源键 Event。因此,您无法使用电源按钮作为返回按钮,或用于一般导航。

按下电源按钮时,用户将返回主屏幕(表盘)。有两个例外情况:

请注意,按电源按钮时,Activity 类的 isFinishing() 函数不返回 true(您也无法截获按键 Event)。

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)