Problems adding TableRow dynamically and getMeasuredHeight / getHeight

I have a problem.

I want to dynamically add lines to TableLayout. When adding these lines, I need to get the size of the view, so I try to do this in time onSizeChanged(), but new lines are not displayed. So I tried onFinishInflate(), but then I do not have access to size (getMeasuredHeight returns 0).

The output of the following codes shows two lines of initial row+ onFinishInflate getMeasuredHeight=0. But if I use hierarchyviewer of sdk and press load view hierarchy, then all of a sudden I see three lines in the emulator initial row+ onFinishInflate getMeasuredHeight=0+ onSizeChanged getMeasuredHeight=270!.

Note. This is using the 2.1 hvga landscape emulator, code created using the latest sdk aimed at android1.5.


<?xml version="1.0" encoding="utf-8"?>
<com.steelbytes.android.TableHeightTest.TestLinLay 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TableLayout
        android:id="@+id/table1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                >
                <TextView
                    android:text="initial row"
                />
            </TableRow>
        </TableLayout>
</com.steelbytes.android.TableHeightTest.TestLinLay>

package com.steelbytes.android.TableHeightTest;

import android.app.Activity;
import android.os.Bundle;

public class TableHeightTest extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

package com.steelbytes.android.TableHeightTest;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TestLinLay extends LinearLayout
{
 Context mContext;

 public TestLinLay(Context context, AttributeSet attrs)
 {
  super(context, attrs);
  mContext = context;
 }

    private void addRow(String funcName)
    {
        TableLayout tl = (TableLayout)findViewById(R.id.table1);
        TableRow tr = new TableRow(mContext);
        TextView tv = new TextView(mContext);
        tv.setText(funcName+" getMeasuredHeight="+tl.getMeasuredHeight());
        tr.addView(tv);
        tl.addView(tr);
    }

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();
        addRow("onFinishInflate");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w,h,oldw,oldh);
        addRow("onSizeChanged");
    }
}

+3
2

, View.measure(int widthMeasureSpec, int heightMeasureSpec) (http://developer.android.com/reference/android/view/View.html#measure%28int,%20int%29) getMeasuredWidth/getMeasuredHeight.

+1

. "": / Layout() " " (onSizeChanged()), - " Runnable", . , , onSizeChanged() ( )

// Note: 'posting a Runnable' is required to call requestLayout(),
// not supposed to call requestLayout() during a 
// layout pass (onSizeChanged())
private Handler handler;        // for posting request(s)

(In constructor code)
{
    handler = new Handler();
}

/*
 * Post request to run a loop requesting button layout so
 * the buttons will be drawn using their new size.
 */
private void postLayoutRequest() {
handler.post(new Runnable() {
    @Override public void run() {
    for(int i = BTN_BASE_ID; i <= TOP_BUTTON_ID; ++i) {
            ButtonEx btn = (ButtonEx)findViewById(i);
            btn.requestLayout();
        }
    }
});
}
+1

Source: https://habr.com/ru/post/1734199/


All Articles