Android Wear uses the same layout technology as the phone, but it needs to be designed for specific situations. Don't copy the UI of your phone directly. More views: Android Wear Design Guidelines
When creating an Android Wear layout, consider that the device has two screens, squares and circles. Any content that is located in the corner of the screen will be clipped by the circular screen.
The wearable UI Library provides two ways to solve this problem:
1. Define two sets of layouts for the round and square screen devices. Detects the device screen at run time and renders a different layout.
2. Use a special layout in the wearable UI library to wrap your layout. This layout will load different layout files according to the device screen.
The typical approach is the first, if the layout is simple can be directly used in the second type.
Add an association for the wearable UI library
If you use the Create Wizard of Android studio to create your project, the wearable UI library will be included in your wear module by default.
' Libs ', include: ['*.jar'com.google.android.support: wearable:+'com.google.android.gms:play-services-wearable:+ ' }
define different layouts for round and square screens
The class watchviewstub in the Wearable UI library allows you to define different layouts for round and square screens. This class detects the screen shape at run time and renders the appropriate layout.
1. Add the watchviewstub element to your activity layout file
2. Use the Rectlayout property to specify the layout file for the square screen
3. Use the Roundlayout property to specify the layout file for the circular screen
<android.support.wearable.view.WatchViewStubxmlns:android="http://schemas.android.com/apk/res/android"Xmlns:app="Http://schemas.android.com/apk/res-auto"Xmlns:tools="Http://schemas.android.com/tools"Android:id="@+id/watch_view_stub"Android:layout_width="match_parent"Android:layout_height="match_parent"App:rectlayout="@layout/rect_activity_wear"App:roundlayout="@layout/round_activity_wear"></android.support.wearable.view.WatchViewStub>@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ wear);}
Accessing layout controls
The render layout file is not synchronized, so a callback is set to listen for the watchviewstub render completion.
@Override protected void = (watchviewstub) Findviewbyid (r.id.watch_view_stub); Stub.setonlayoutinflatedlistener (New public void onlayoutinflated (watchviewstub stubs) {// now You can access yourviews TextView TV = (TextView) Stub.findviewbyid (R.id.text); ...});}
using the Shape-aware layout
The Wearable UI library contains classes Boxinsetlayout inherited from Framelayout, allowing you to define a single layout for both square and round screens
In order to display this area, the sub view needs to set the Layout_box property, top, bottom, left and right can be used together, for example "Left|top"
In the square screen, the Layout_box property is ignored.
<android.support.wearable.view.BoxInsetLayoutxmlns:android="http://schemas.android.com/apk/res/android"Xmlns:app="Http://schemas.android.com/apk/res-auto"Android:background="@drawable/robot_background"Android:layout_height="match_parent"Android:layout_width="match_parent"android:padding="15DP"><Framelayoutandroid:layout_width="match_parent"Android:layout_height="match_parent"android:padding="5DP"App:layout_box=" All"><textviewandroid:gravity="Center"Android:layout_height="wrap_content"Android:layout_width="match_parent"Android:text="@string/sometext"Android:textcolor="@color/black"/><Imagebuttonandroid:background="@null"android:layout_gravity="Bottom|left"Android:layout_height="50DP"Android:layout_width="50DP"android:src="@drawable/ok"/><Imagebuttonandroid:background="@null"android:layout_gravity="Bottom|right"Android:layout_height="50DP"Android:layout_width="50DP"android:src="@drawable/cancel"/></framelayout></android.support.wearable.view.boxinsetlayout>
Definition layout of android-wear development