2015-11-29 28 views
1

我的活動應將屏幕分爲兩部分。第一個將保留佔用儘可能多空間的自定義視圖。第二部分包含按鈕矩陣。我在設計時沒有他們的電話號碼,我想根據屏幕尺寸和其他條件在Activity中創建它們(簡單的級別將具有較少的按鈕)。Android - 根據測量前的活動計算尺寸

enter image description here

我目前使用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()。要繼續,我必須計算可用空間,按鈕數量和兩部分之間的空間,並將此信息傳遞給TilesViewLayoutManagers。不幸的是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 
+0

您是否嘗試在onCreateView()中進行計算? – noev

+0

活動 –

+0

中沒有這種方法啊,對不起,那是我動態創建複雜佈局的地方,但我使用了片段。 – noev

回答

1

您是否確切知道底部矩陣有多少行?

您可以設置android:layout_weight="1"android:layout_height="0px" 告訴ViewGroup您customView將佔據全部排除按鈕矩陣中的剩餘空間。

+0

它看起來很有希望,謝謝。第二部分現在顯示。但是我必須計算第一部分中與第二部分中的按鈕數對應的圖塊數量。如果我不知道屏幕的寬度和高度,我無法想象如何去做。 –

+0

你已經知道如何獲得屏幕的寬度和高度了嗎?我的小建議是,你可以將你的高度和寬度存儲到Application類的一個靜態變量中,這樣你只需要計算一次。您可以將兩個視圖合併爲一個更多的自定義視圖,並實現自己的視圖佈局(重寫onMeasure())。 –

+0

我更新了我的問題。我可以獲得顯示尺寸,但比實際的內容視圖尺寸稍大。 –

1

關於第一個問題:

在的onCreate()的生命週期,你不能得到的尺寸。 因爲你所有的觀點都沒有測量過()!我總是使用view.post()來獲取onCreate()方法中的dimension()。

view.post(new Runnable() { 
     @Override 
     public void run() { 
      view.getMeasuredHeight(); 
     } 
    }); 

關於第二個問題:

在底部,我認爲你應該使用自定義視圖擴展ViewGroup.Override構造,onMeasure(),onLayout():

public class TextBlocksView extends ViewGroup {} 

在構造函數,您添加LinearLayout(方向:水平)儘可能多的行號,並添加每個項目(addView()方法和layoutParams重量1 !!)。...

public TextBlocksView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    this.removeAllViews(); 
    ..... 
    this.addView(linearLayoutView); 
} 

在onMeasure(),你在這個地方計算你的尺寸(溫馨提示:注意dipToPx方法!)。

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    View childAt = this.getChildAt(0); 
    int measuredHeight = childAt.getMeasuredHeight(); 
    int measuredWidth = childAt.getMeasuredWidth(); 
    childAt.layout(left, top, right, bottom); 
    ....... 
} 
在onLayout()

,你把你的訂單項(LinearLayout中方向:水平)!

在頂部。你可以用同樣的方法!

我的母語是中國人,我不擅長英語。希望有些人正確編輯我的答案!非常感謝。

+0

你的英語非常好!你的回答有一個有趣的觀點,但雞雞蛋仍然沒有解決。我需要活動來計算兩個部分的尺寸,尤其是第二部分。該活動將創建節的按鈕,因此它不能等待測量完成。 –