java - 像在 Android 的 Facebook 应用程序中一样创建登录布局

标签 java android xml layout

我想制作一个具有类似于 Facebook 应用程序的登录 Activity/布局的应用程序。我的意思是,当文本字段聚焦时,软键盘会将整个 View 向上推,但不会压扁 Logo 。我已经尝试过 android:windowSoftInputMode="adjustPan/adjustResize" 但这不是我想要实现的目标。

我找到了 this关于 SO 的问题也许会让事情变得更清楚,但它没有解决问题的方法。

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

更新:

enter image description here enter image description here

enter image description here enter image description here

    <?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. 

这是 Activity (键盘上/下)的 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" 作为登录名和密码 EditText 来删除输入建议并保存一些像素。在 Activity android:windowSoftInputMode="adjustResize|stateHidden" 的 list 中。 stateHidden 用于在 Activity 加载时键盘不会启动。希望能帮助到你。祝你好运。

最佳答案

他们使用相对布局、adjustResize 和 android:layout_centerVertical 来实现。基本上,他们的主要布局有一个线性布局,其中有 3 个等权重的相对布局。每个都设置为 0dp 高度,因此它们占据屏幕的三分之一。顶部的 RelativeLayout 包含 Logo ,垂直居中。中间是登录字段和按钮,一个在另一个上面垂直居中。底部的是版权文本,与底部对齐。最终结果是,当键盘出现时,3 个相关布局会调整大小以占据新屏幕的 1/3。然后它们的元素在新屏幕中居中。

请记住,您需要使用 adjustResize 窗口模式来实现此功能,如果您使用平移,它只会向上移动并且 Logo 会滚动到偏离中心的位置。

关于java - 像在 Android 的 Facebook 应用程序中一样创建登录布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16050228/

相关文章:

java - 调用restTemplate.getForObject时找不到数据如何处理

java.lang.ClassCastException : android.graphics.drawable.ColorDrawable无法转换为android.graphics.drawable.BitmapDrawable

android - 如何在 android jetpack compose 的非全屏横向模式下使用背景颜色为切口/缺口区域着色

android - GoogleSignInClient 返回 8(内部错误)

java - 谁能给我一份无效的 XML 字符列表

Java OpenCV haarcascade_frontalface_default.xml

java - 如何构建 Sparql 查询来获取带空格的字符串

java - 15, 11 汉明码发生器矩阵

android - 接到电话后应用程序崩溃

html - 在 Ruby(或 Shell)中漂亮地打印 HTML