0

我填充方法TableLayoutOnCreate(..)我想获得表格每一列的宽度。问题是在OnCreate(...)方法视图中将宽度设置为 0。收集表格宽度的正确位置在哪里?

我也尝试过OnStart(...)(来自文档“当活动对用户可见时调用。”)但结果是一样的。

更简单的示例显示了问题(与上面的描述不同):

package xliiv.hello;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

        tv = (Button) findViewById(R.id.button1);
        int width = tv.getWidth();
        //this width will be 0! I expect that textview widths is not 0
        Log.i("tag", "" + width);
    }
4

1 回答 1

1

你应该这样做onWindowFocusChanged(),这将给出确切的宽度

试试这个:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);

    int width = findViewById(R.id.button1).getWidth();

    Log.i("tag", "" + width);

}
于 2012-06-13T10:50:00.533 回答