2012-02-19 29 views
-1

我試圖建立一個RPN計算器添加視圖,直到有沒有更多的空間

enter image description here

鍵盤收縮和拉伸相當好,所以我想棧行添加到視圖(藍色選擇)

3:  0 
2:  0 
1:  0 

直到屏幕/視圖沒有更多空間。

第一行的xml,它的容器是。

<LinearLayout 
    android:id="@+id/stackLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="bottom|fill_vertical" 
    android:layout_weight="1" 
    android:gravity="bottom" 
    android:orientation="vertical" > 

<!-- Stack row --> 
    <LinearLayout 
     android:id="@+id/stackRow" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="bottom" 
     > 
     <!-- Row index --> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="1:" 
      style="@style/text_style" /> 
     <!-- Value --> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_vertical|right" 
      android:singleLine="true" 
      android:text="0" 
      style="@style/text_style" /> 
    </LinearLayout> 
</LinearLayout> 

我在嘗試添加元素失敗的嘗試是在activitys的OnCreate

// Setup table rows for value stack 
LinearLayout stackView = (LinearLayout) findViewById(R.id.stackLayout); 
int heightPx = stackView.getHeight(); 
LinearLayout stackRow = (LinearLayout) findViewById(R.id.stackRow); 
int rowHeightPx = stackRow.getHeight(); 
// Space for additional rows 
int heightLeftPx = heightPx - rowHeightPx; 

//stackView.removeView(stackRow); 
// Add rows to fit 
int count = 0; 
while (heightLeftPx > rowHeightPx) { 
    count++; 
    // add new LinearLayout row 
    heightLeftPx-= rowHeightPx; 
} 

的stackView不斷報告其爲0一樣的行高。 我哪裏錯了?

我已經嘗試將此移至onStart,onResume和onPostResume以獲得相同的結果。

回答

0

我需要添加到我的活動的onCreate:

new AnInnerSillyWaitToCompleteLayoutClass().execute((Void[]) null); 

然後在我的活動我有一個:

class AnInnerSillyWaitToCompleteLayoutClass extends AsyncTask<Void, Void, Void> 
    protected Void doInBackground(Void... voids) { 
     sleep a little while 

    protected void onPostExecute(Void result) { 
     Here the layout is complete and the dynamic 
     dimensions can be calculated. 

編輯:正確的方法是偵聽佈局的變化,以及實現這些佈局完成後的操作。

0

據我所知,視圖還沒有呈現在oncreate?也許在簡歷中他們是?

+0

它們在OnCreate,OnStart和OnResume中產生0。 – 2012-02-19 20:15:03

相關問題