【发布时间】:2015-02-17 06:56:40
【问题描述】:
我有一个设置了“adjustPan”选项的活动,当 EditText 获得焦点并显示软键盘时,它可以很好地平移我的视图,但我的问题是我的 EditText 下方有 UI 元素需要当键盘显示并且不在较小的屏幕上时也可见。我可以告诉视图始终平移,以便通过将它们链接到 EditText 或其他东西来显示这些 UI 元素,还是我必须手动控制平移并计算高度等等,或者这里有什么好的解决方案?
【问题讨论】:
我有一个设置了“adjustPan”选项的活动,当 EditText 获得焦点并显示软键盘时,它可以很好地平移我的视图,但我的问题是我的 EditText 下方有 UI 元素需要当键盘显示并且不在较小的屏幕上时也可见。我可以告诉视图始终平移,以便通过将它们链接到 EditText 或其他东西来显示这些 UI 元素,还是我必须手动控制平移并计算高度等等,或者这里有什么好的解决方案?
【问题讨论】:
你可以做的是你可以使用 ViewTreeObserver 来平移你的内容。 这是我的应用程序。
int edtHeight = 0;
// llContent is the Viewgroup layout which is inside a ScrollView.
ViewTreeObserver viewTreeObserver1 = llContent.getViewTreeObserver();
viewTreeObserver1.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
// Here edtHeight will get the height of edittext as i wanted my view to scroll that much bit more
edtHeight = edtTemprature.getHeight();
ViewTreeObserver obs = llContent.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
//llMain is the parent View Group of my XML layout
llMain.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
llMain.getWindowVisibleDisplayFrame(r);
int heightDiff = llMain.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100)
{ // if more than 100 pixels, its probably a keyboard...
if(edtTemprature.hasFocus()){
llContent.scrollTo(0, (int) (edtHeight * 1.5));
}else{
llContent.scrollTo(0, 0);
}
}
else
{
llContent.scrollTo(0, 0);
}
}
});
【讨论】:
在 Android manifestfile 中使用 stateHidden 你需要哪个类..
android:windowSoftInputMode="stateHidden"
否则,
在活动中,
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
【讨论】: