什么是获得布局高度的最佳位置?

时间:2011-07-20 17:27:59

标签: android android-layout

我正在尝试将Padding Top设置为另一个View的指定Layout Height,但Layout一直给我0值。 我试图将onCreateonStart以及onResume中的代码设置为相同的值。

代码示例:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_screen_layout);

        EditText editText = (EditText) findViewById(R.id.editText1);
        editText.setPadding(0, findViewById(R.id.main_screen_banner_linearLayout).getBottom(), 0, 0);

        System.err.println("Bottom:" + findViewById(R.id.main_screen_banner_linearLayout).getHeight() + "");
    }
}

===== 关于C尼克答案:

感谢@C Nick 我通过他的回答找到了解决方案:

public class CustomTextView extends TextView {
    Runnable runnable;

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (runnable != null) {
            runnable.run();
        }
    }
}
onCreate

Activity

    final CustomTextView customTextView = (CustomTextView) findViewById(R.id.editText1);
    customTextView.setRunnable(new Runnable() {

        @Override
        public void run() {
            System.out.println("Height:" + findViewById(R.id.main_screen_banner_linearLayout).getHeight());
            customTextView.setPadding(customTextView.getPaddingLeft(),
                    findViewById(R.id.main_screen_banner_linearLayout).getHeight(),
                    customTextView.getPaddingRight(), customTextView.getPaddingBottom());
        }
    });

现在它工作正常,但我不认为我的解决方案是最佳的

1 个答案:

答案 0 :(得分:2)

您需要在布局传递发生后的某个时间设置填充。您可以概括地覆盖EditText的onLayout,并设置填充,因为View应该被布局并在那时进行测量。

这是布局/测量/绘制工作流程的一个不错的资源。

How Android Draws

相关问题