我的活動應將屏幕分爲兩部分。第一個將保留佔用儘可能多空間的自定義視圖。第二部分包含按鈕矩陣。我在設計時沒有他們的電話號碼,我想根據屏幕尺寸和其他條件在Activity
中創建它們(簡單的級別將具有較少的按鈕)。Android - 根據測量前的活動計算尺寸
我目前使用LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:id="@+id/puzzleScreen"
android:layout_width="match_parent" android:layout_height="match_parent">
<lelisoft.com.lelimath.view.TilesView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tiles" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/choices">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="23"/>
</LinearLayout>
</LinearLayout>
TilesView
消耗了整個屏幕,直到我重寫onMeasure()
。要繼續,我必須計算可用空間,按鈕數量和兩部分之間的空間,並將此信息傳遞給TilesView
和LayoutManagers
。不幸的是onCreate()
是這個計算不正確的位置,因爲我無法弄清根視圖的尺寸(獲得者返回0)。
活動生命週期中哪裏是獲取維度並動態創建所有按鈕的地方?在我看來,像雞蛋問題。
UPDATE
我試圖調試其他方法:onStart()
和onResume()
返回0,然後測量被稱爲最後onCreateOptionsMenu()
接收座標。太晚了。
UPDATE 2
的問題是如何找出從活動測量之前的內容查看的尺寸。我可以得到getWindowManager().getDefaultDisplay()
,但它包含了操作欄等
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_puzzle);
View puzzleView = findViewById(R.id.puzzleScreen);
// todo get maximum width and height for R.layout.activity_puzzle
您是否嘗試在onCreateView()中進行計算? – noev
活動 –
中沒有這種方法啊,對不起,那是我動態創建複雜佈局的地方,但我使用了片段。 – noev