我想在RelativeLayout中的特定位置上設置我的EditTest。所以我得到Ediitext的位置,並根據我想要的設置。這對我來說工作得很好。但是當Android 2.2和android 2.3設備沒有找到getY()
方法時會出現問題。因爲我想獲得Ediitext的位置。如何獲取EditText在RelativeLayout中的位置或替代方法getY()
現在我需要找到另一種方法,因爲它不適用於2.2和2.3。
這裏是我的代碼供參考。
ViewTreeObserver autoCompleteObserver = myAutoComplete.getViewTreeObserver();
autoCompleteObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
relativeLayoutHeight = ((int) myAutoComplete.getY()-12);
}
});
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
Log.i("TEST", "GLOBAL LAYOUT "+activityRootView.getHeight());
Log.i("TEST", "GLOBAL LAYOUT rel"+relativeLayoutHeight);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
Log.i("TEsT","Keyboard visible");
myRelativLayout.scrollTo(0, relativeLayoutHeight);
}
else
{
Log.i("TEsT","Keyboard not visible");
myRelativLayout.scrollTo(0, 0);
myAutoComplete.setDropDownHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
});
}
這段代碼在4.1工作正常,但我需要找到另一種方法爲getY()
這回我的EditText的位置。
給我任何參考或提示。
View.getTop()? – Blackbelt