我很想知道磨损类型是圆形还是方形,因为基于设备类型,我想对圆形和方形有不同的看法。从This Post我知道在SDK-20中android.view.View
类有一个新的监听器来检测设备类型,使用这种方法我试图获取设备类型,但是在logcat上没有打印任何内容。下面是我尝试的代码。
public class WearSplash extends Activity implements OnApplyWindowInsetsListener {
private WatchViewStub _stub = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear_splash);
_stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
_stub.setOnApplyWindowInsetsListener(this);
_stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub mStub) {
// here on the basis of device type i want to inflate views.
}
});
}
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
System.out.println(" in testing phase");
if (insets.isRound())
System.out.println("in round");
else
System.out.println("in square");
return null;
}
}
xml文件。
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub xmlns: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"
app:roundLayout="@layout/round"
tools:context="com.app.WearSplash"
tools:deviceIds="wear" >
</android.support.wearable.view.WatchViewStub>
圆形的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.app.WearSplash"
tools:deviceIds="wear_round" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="@string/hello_world" />
</RelativeLayout>
正方形:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.app.WearSplash"
tools:deviceIds="wear_square" >
<ImageView
android:id="@+id/image"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:contentDescription="@string/hello_world"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
我是新手,不知道要怎么做。援助之手将不胜感激。
最佳答案
非官方的方式(无需使用WatchViewStub)-但对我来说,这是更容易的方式。 https://github.com/tajchert/ShapeWear
只需复制ShapeWear.java类,并订阅屏幕形状检测事件setOnShapeChangeListener()
或调用方法ShapeWear.isRound()
(可能尚未确定形状就引发错误)或ShapeWear. getShape()
-在相同情况下可能会导致ShapeWear.SHAPE_UNSURE
。