编辑:要问问"为什么它在setOnClickListener"中有效,但很快就找到了。 https://stackoverflow.com/a/21926714/6474462
旧: 正在玩ObjectAnimator并注意到LinearLayout getHeight()返回0,但是在setOnClickListener(点击后)内部确实返回了真值,这是如何工作的?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main_linear);
TextView textView1 = (TextView) findViewById(R.id.textview1);
final TextView textView2 = (TextView) findViewById(R.id.textview2);
int viewHeight1 = linearLayout.getHeight();
textView1.setText("viewHeight1: " + viewHeight1);
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int viewHeight2 = linearLayout.getHeight();
textView2.setText("viewHeight2: " + viewHeight2);
}
});
}
}
答案 0 :(得分:2)
您需要在屏幕上的对象绘制后调用getHeight()
@echo off