6

我想制作一个登录活动/布局类似于 Facebook 应用程序的应用程序。我的意思是当文本字段聚焦时,软键盘会向上推动整个视图,但不会挤压徽标。我已经尝试过android:windowSoftInputMode="adjustPan/adjustResize",但这不是我想要达到的目标。

我在 SO 上发现了这个问题,也许它会让事情变得更清楚,但它没有解决问题的办法。

我也尝试过各种布局类型,但它的软键盘只会将焦点 < EditText > 向上推。请指导我。

更新:

在此处输入图像描述在此处输入图像描述

在此处输入图像描述在此处输入图像描述

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#DDDDDD">
    <RelativeLayout 
        android:height="0dp"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_width="fill_parent"
        android:background="#ff0000">
        <ImageView
            android:layout_centerVertical="true"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"></ImageView>
    </RelativeLayout>
    <RelativeLayout 
        android:height="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:background="#00ff00">
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#0000ff"
        android:height="0dp" >

        <Button
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Log in" 
            />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="4dp"
            android:hint="password"
            android:inputType="textPassword" >
        </EditText>

        <EditText
            android:id="@+id/editText1"
            android:hint="login"
            android:padding="4dp"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" ></EditText>
    </RelativeLayout>

</LinearLayout>

更新工作解决方案 我不能在这里粘贴整个 xml 文件,但结构应该足够了。基于 Gabe Sechan 的回答。

Layout{
   Layout top weight 1
   Layout mid weight 1
   Layout bot weight 1
}

子布局已设置为:

android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"  // should be changed accordingly to your layout design. 

这是活动的Java代码(键盘向上/向下):

View top, mid, bot;
    final View activityRootView = findViewById(R.id.loginLayout);
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
                    new OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            int heightDiff = activityRootView.getRootView()
                                    .getHeight() - activityRootView.getHeight();
                            if (heightDiff > 100) { //keyboard up
                                mid.setVisibility(View.INVISIBLE);
                                top.setLayoutParams(new TableLayout.LayoutParams(
                                        LayoutParams.MATCH_PARENT, 0, 0f));
                                bot.setLayoutParams(new TableLayout.LayoutParams(
                                        LayoutParams.MATCH_PARENT, 0, 1f));
                                
                            } else {// keyboard down
                                // v.setVisibility(View.VISIBLE);
                                mid.setVisibility(View.VISIBLE);
                                top.setLayoutParams(new TableLayout.LayoutParams(
                                        LayoutParams.MATCH_PARENT, 0, 2f));
                                bot.setLayoutParams(new TableLayout.LayoutParams(
                                        LayoutParams.MATCH_PARENT, 0, 3f));
                                
                            }
                        }
                    });
   
       

在键盘向上时,您需要根据键盘向上设计更改权重,在键盘向下时更改回默认值(您通过 xml/java 设置的布局)。我已经在 2.3.x 及更高版本上测试了代码。并且不要忘记使用android:inputType="textFilter"login&passwordEditText来删除输入建议并保存一些像素。在您的活动清单中android:windowSoftInputMode="adjustResize|stateHidden"stateHidden用于使键盘在活动加载时不会启动。希望能帮助到你。祝你好运。

4

2 回答 2

8

They're doing it with relative layouts, adjustResize, and android:layout_centerVertical. Basically, they have a linear layout for their main layout, with 3 equally weighted relative layouts inside of it. Each is set to 0dp height, so they take up equal thirds of the screen. The top RelativeLayout holds the logo, centered vertically. The middle holds the login fields and button, centered vertically one on top of the other. The bottom one holds the copyright text, aligned to bottom. The end result is that when the keyboard comes up, the 3 relative layouts get resized to take 1/3 of the new screen. Then their elements are centered in the new screen.

Remember you need the adjustResize window mode to get this, if you use pan it will just move up and the logo will scroll off center.

于 2013-04-17T03:36:23.243 回答
0

在 Eclipse 中,转到 File|New|Other,然后在随后的向导中,选择 Android Activity,然后在下一页上,从活动列表中选择 LoginActivity。这具有您正在谈论的确切布局,您可以将其用作框架。它使用 ScrollView 来实现您正在寻找的效果。

于 2013-04-17T04:31:47.420 回答