添加视图,直到没有空间

时间:2012-02-19 19:29:08

标签: android android-layout

我正在尝试构建一个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>

尝试添加元素的失败尝试位于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,结果相同。

2 个答案:

答案 0 :(得分:0)

据我所知,在oncreate中还没有渲染视图?也许在简历上他们是?

答案 1 :(得分:0)

我需要添加到我的Activity 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.

编辑:正确的方法是监听布局更改,并在布局完成后实施这些操作。