Android-全局布局侦听器不起作用 [英] Android - Global Layout Listener not working
本文介绍了Android-全局布局侦听器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在此问题.当我将其粘贴到代码中时,在addOnGlobalLayoutListener(new OnGlobalLayoutListener()
,onGlobalLayout()
和activityRootView
(在heightDiff之后)下方出现红色的弯曲.请帮助我找出问题所在.
I used the code from Reuben Scratton's new answer in this question. When I paste it into my code, I get red squigglies underneath addOnGlobalLayoutListener(new OnGlobalLayoutListener()
, onGlobalLayout()
, and activityRootView
(after heightDiff). Please help me figure out what is wrong.
谢谢 这是我在.java页面上的代码
Thanks Here's my code on the .java page
public class Details extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
}
final View activityRootView = findViewById(R.id.details);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
ImageButton backButton = (ImageButton)findViewById(R.id.back);
backButton.setImageResource(R.drawable.hide_keyboard);
}
}
});
推荐答案
您需要将该代码添加到方法中,例如onCreate()
.
You need to add that code inside a method, like onCreate()
.
public class Details extends Activity {
View activityRootView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
activityRootView = findViewById(R.id.details);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
ImageButton backButton = (ImageButton)findViewById(R.id.back);
backButton.setImageResource(R.drawable.hide_keyboard);
}
}
});
}
}
这篇关于Android-全局布局侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文