为什么 TextView.getHeight() 给出不同的值?
Why TextView.getHeight() gives different values?
我有一个带有 textView 和 Button 的应用程序,当调用 onCreate 时,我调用 textView.getHeight() 并且返回的值为 0。(也在 onResume 和 onPostResume 中尝试过)。
当用户点击按钮时,它再次调用 textView.getHeight(),现在我得到的答案是 864 而不是 0。
为什么会这样?
(布局只有一个 TextView 和一个 Button - 父级是 RelativeLayout)
edit: TextView 在 xml 中有:android:layout_height="match_parent"(父级是 RelativeLayout,布局的基础父视图),所以真实高度必须是> 0.
代码:
TextView text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (AutoScrollingText) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostResume() {
super.onPostResume();
Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
switch(view.getId()){
case (R.id.button) : {
Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
}
}
}
使用这个
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (AutoScrollingText) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
text.post( new Runnuble(){
Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
});
}
它应该有效
我有一个带有 textView 和 Button 的应用程序,当调用 onCreate 时,我调用 textView.getHeight() 并且返回的值为 0。(也在 onResume 和 onPostResume 中尝试过)。
当用户点击按钮时,它再次调用 textView.getHeight(),现在我得到的答案是 864 而不是 0。 为什么会这样?
(布局只有一个 TextView 和一个 Button - 父级是 RelativeLayout)
edit: TextView 在 xml 中有:android:layout_height="match_parent"(父级是 RelativeLayout,布局的基础父视图),所以真实高度必须是> 0.
代码:
TextView text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (AutoScrollingText) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostResume() {
super.onPostResume();
Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
switch(view.getId()){
case (R.id.button) : {
Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
}
}
}
使用这个
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (AutoScrollingText) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
text.post( new Runnuble(){
Toast.makeText(this, "text height: " + text.getHeight(), Toast.LENGTH_SHORT).show();
});
}
它应该有效