3

問題があります。

に動的に行を追加したいTableLayout。これらの行を追加するときは、ビューのサイズを取得できる必要があるため、実行中にこれを実行しようとしましonSizeChanged()たが、新しい行が表示されません。だから試してみonFinishInflate()ましたが、サイズにアクセスできません(getMeasuredHeight returns 0)

次のコードの出力は、2行initial row+を示していますonFinishInflate getMeasuredHeight=0。しかし、SDKからhierarchyviewerを使用してを押すとload view hierarchy、エミュレーターに突然3行が表示されますinitial row+ onFinishInflate getMeasuredHeight=0+ onSizeChanged getMeasuredHeight=270!。

注:これは、エミュレータ2.1 hvgaランドスケープ、最新のsdkでビルドされたコード、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");
    }
}

4

2 に答える 2

1

私も同じ問題を抱えていました。最終的に、次の「ルール」に出くわしました。「レイアウトパス」(onSizeChanged())中にrequestLayout()を要求できない/すべきではありません。正しい方法は、「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();
        }
    }
});
}
于 2011-11-22T15:48:05.573 に答える
1

View.measure(int widthMeasureSpec, int heightMeasureSpec)( http://developer.android.com/reference/android/view/View.html#measure%28int,%20int%29 ) を呼び出す前に、測定された高さを取得します。 getMeasuredWidth/getMeasuredHeight.

于 2011-01-14T03:16:26.430 に答える