Android 如何显示键盘上的调整活动
我有一个伪装成对话的活动。 我想在显示键盘时调整活动 我的布局资源:Android 如何显示键盘上的调整活动,android,android-layout,Android,Android Layout,我有一个伪装成对话的活动。 我想在显示键盘时调整活动 我的布局资源: RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent"> <LinearLayou
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
...
</ScrollView>
</LinearLayout>
清单中没有任何可以实现此目的的设置,您必须在布局末尾的布局中添加额外的高度(您想要的)和宽度0视图。当键盘出现时,只需使其可见即可。要查找键盘的状态,请使用此
int lastDiff = 0;
public static boolean keypadopen;
在onCreate()中添加以下内容
addKeyBoardHandler();
它将根据键盘的当前状态设置boolean keypadopen。如果为真,则添加所需高度的额外视图,该视图将向上滚动
private void addKeyBoardHandler()
{
final View activityRootView = findViewById(R.id.container);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout()
{
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (lastDiff == heightDiff) return;
lastDiff = heightDiff;
System.out.println("Keypad height :"+heightDiff);
if (heightDiff > 100)
{
keypadopen = true;
keypadListner.onKeypadOpen(heightDiff+10);
}
else
{
keypadopen = false;
keypadListner.onKeypadOpen(heightDiff+10);
}
}
});
其中container是根视图的id。有时android:theme=“@android:style/theme.transparent.NoTitleBar.Fullscreen”会导致一些问题,并且窗口的大小没有调整。删除它以查看它是否有效,然后检查是否有替代方案。我在Android Developer上找到了这个,但它不起作用。。。是的,我知道,但是你试过上面的方法吗?毫无疑问,它会起作用的。什么是Rect。。。?我错过了一些东西,比如键盘打开,键盘列表,lastDiff到workint lastDiff=0;公共静态布尔键打开;将这个addKeyBoardHandler()添加到onCreate()中;它将使用键盘的当前状态设置boolean keypadopen。如果为真,则添加所需高度的额外视图,该视图将向上滚动。Rect是矩形,用于获取视图中的更改,以便我们可以识别该键盘状态。问题是android:theme=“@android:style/theme.Transparent.NoTitleBar.Fullscreen”
private void addKeyBoardHandler()
{
final View activityRootView = findViewById(R.id.container);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout()
{
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (lastDiff == heightDiff) return;
lastDiff = heightDiff;
System.out.println("Keypad height :"+heightDiff);
if (heightDiff > 100)
{
keypadopen = true;
keypadListner.onKeypadOpen(heightDiff+10);
}
else
{
keypadopen = false;
keypadListner.onKeypadOpen(heightDiff+10);
}
}
});