我正在尋找一種技術來確定Java Wear設備屏幕是圓形還是矩形。請注意,這不僅僅是佈局;我的代碼實際上需要知道它處理的是哪個形狀,因爲它們的處理方式不同。根據我在網上的代碼示例中看到的,兩種不同的方法應該是可能的 - 但我一直無法讓他們中的任何一個人工作。我會將它們包含在這裏以消除它們的運行,或者可能的故障排除(如果任何人都可以看到它們的問題)。請不要將我引薦給另一個SO職位,只是重申在這裏不適合我的解決方案。以編程方式確定Android Wear中的屏幕形狀
請注意,這裏的所有代碼都在手錶上運行。另外,我仍然在使用Eclipse,FWIW。
我見過的最簡單的方法是在我的佈局中添加一個onApplyWindowInsets()
偵聽器。在我的onCreate()方法
view.setOnApplyWindowInsetsListener(this);
:所以我創建了一個監聽器,看起來像這樣:
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (insets.isRound()) {
displayShape = "round";
} else {
displayShape = "rectangular";
}
return null;
}
,並把它添加到我的佈局與代碼根視圖這樣。看起來沒問題 - 但聽衆永遠不會被叫到。我還發現一些建議,說我需要手動調用它,因此:
view.requestApplyInsets();
但這似乎沒有任何區別。我已經嘗試過將它放在不同的視圖,不同的生命週期方法等等,但從來沒有看到它實際上被我的應用程序調用。這是在我的LG G手錶上運行,BTW。
第二種方法是一種黑客攻擊,它基於已發佈的WatchViewStub
輔助類。我通過籃球躍升得到導入到Eclipse項目中的可穿戴的支持庫,然後添加以下到我的根佈局:
<android.support.wearable.view.WatchViewStub
android:id="@+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect"
app:roundLayout="@layout/round"
/>
創造rect.xml
這樣:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_type"
android:text="rectangular"
/>
和round.xml
這樣:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_type"
android:text="round"
/>
最後,在我的onCreate()
我添加下面的Java代碼:
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
TextView layoutType = (TextView) findViewById(R.id.layout_type);
displayShape = layoutType.getText().toString();
}
});
這是一段很長的路,但它應該工作,對吧?沒有那麼多...... displayShape
始終設置爲"rectangular"
,表明它始終使用rect.xml
,即使在輪循模擬器上運行也是如此。 [我沒有經過全面篩選的硬件,現在就試用它。]
那麼,有沒有人看到我在這兩種方法中哪一種出錯?或者你能建議第三種方法嗎?
感謝您的反饋。我會研究這個主題,看看在這裏可以做些什麼。 – 2014-10-16 06:27:19
對於資源限定符來說,手錶顯示更多的不僅僅是圓形和方形。 Moto 360在圓形顯示屏的底部有一個插圖,而圓形模擬器沒有插頁。所以他們在那裏也不一樣。所以我們使用WindowInsets https://developer.android.com/reference/android/view/WindowInsets.html來編碼顯示器之間的差異。 – 2014-10-16 06:29:04
這絕對是我沒有調用「OnApplyWindowInsetsListener.onApplyWindowInsets()」方法時遇到的問題。 根本原因是我正在構建一個基於'android:Theme'使用主題'GdxTheme'的_libgdx_項目。當我將'GdxTheme'改爲從'@android:style/Theme.DeviceDefault'繼承時,調用了insets回調方法。 這裏是固定版本的'GdxTheme': ''style name =「GdxTheme」parent =「@ android:style/Theme.DeviceDefault」>' – outofcoffee 2014-12-21 19:48:22