2014-07-23 21 views
8

我很想知道磨損類型,無論是圓形還是方形,因爲根據設備類型我想爲圓形和方形制作不同的視圖。從This Post我知道在SDK-20 android.view.View類有一個新的偵聽器來檢測設備類型,使用此我試圖獲取設備類型,但沒有打印logcat上。以下是我試過的代碼。Android Wear類型檢測 - 圓形或方形

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> 

我是新穿的發展,讓我知道我錯了。幫助雙手將受到高度讚賞。

+0

不知道它是這樣的問題,我還沒有試過穿要麼,但是當我試圖執行'OnApplyWindowInsetsListener'在Android Studio中,它被認爲是自動完成的'View.OnApplyWindowInsetsListener'。所以你可以試試'公共類WearSplash擴展活動實現View.OnApplyWindowInsetsListener'而不是'類WearSplash擴展活動實現OnApplyWindowInsetsListener'。 –

+0

已經嘗試過,不能正常工作 – Dev

+0

實際上,當我試圖在沒有View.'部分的情況下實現'OnApplyWindowInsetsListener'時,出現錯誤。 –

回答

7

非官方的方式(不使用WatchViewStub) - 但對我來說,這是方式更容易。 https://github.com/tajchert/ShapeWear

只需複製ShapeWear.java類和訂閱篩選形狀檢測事件setOnShapeChangeListener()或調用方法ShapeWear.isRound()(可以拋出錯誤形狀尚未確定)或ShapeWear. getShape() - 這可能會導致在同樣的情況ShapeWear.SHAPE_UNSURE

1

使用WatchViewStub來擴大視圖。在佈局文件中,您可以爲每種手錶類型指定佈局,如下所示。它會根據設備的要求自動選擇正確的佈局。

<android.support.wearable.view.WatchViewStub 
    . 
    . 
    . 
    app:rectLayout="@layout/rect_activity_my_wear" 
    app:roundLayout="@layout/round_activity_my_wear" 
    . 
    . 
    . 
> 
1

如果您需要以編程方式獲取此信息,你是隻需添加一個監聽setOnApplyWindowInsetsListener()正確。但是,爲了觸發這一點,您必須請致電requestApplyInsets()

在任何情況下,如果您只想知道在兩種可選佈局之間切換,您可以使用自動執行此操作的WatchViewStub類(文檔號here)。

+0

你能告訴我如何在我的代碼中實現這個來獲得磨損類型嗎? – Dev

+0

我不認爲有必要手動調用'requestApplyInsets()'。我已經成功地讓這個聽衆不用它了。 –

+0

@MaciejCiemięga在'WatchViewStub'上,或與其他View子類一起使用? – matiash