【问题标题】:keyboard hides the layout components键盘隐藏布局组件
【发布时间】:2016-07-03 14:26:12
【问题描述】:

如何防止键盘在弹出时隐藏布局?我正在使用这行代码在键盘打开时调整布局大小。

Window.SetSoftInputMode(SoftInput.AdjustResize);

结果是,虽然某些设备的屏幕大小会调整,但它并没有达到我想要的其他设备的程度。例如,这是布局在一台设备上的样子:

这是它在另一台设备上的外观:

如您所见,在第二张图片中,登录按钮是半可见的,因为键盘与其重叠。顶部的“登录”文本也被截断。如何使所有设备的布局看起来都一样?

编辑

我的 Menifest 文件中也有这一行:

<activity android:name=".AccountActivity" android:label="yourtime" android:windowSoftInputMode="adjustResize|stateHidden"></activity>

这是布局的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:weightSum="100"
android:id="@+id/signinwrapper"
android:background="#F73494">
<LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/linearLayout2"
    android:layout_weight="5"
    android:gravity="center">
    <TextView
        android:text="Login"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:textAllCaps="true"
        android:textColor="#FFFF"
        android:textSize="14sp" />
</LinearLayout>
<LinearLayout
    android:paddingTop="20dp"
    android:orientation="horizontal"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="40"
    android:id="@+id/linearLayout1">
    <ImageView
        android:src="@drawable/logo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView1" />
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/linearLayout2"
    android:layout_weight="15"
    android:paddingTop="10dp">
    <TextView
        android:text="Your home of Entertainment"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textView1"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:textAllCaps="true"
        android:textColor="#FFFF" />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/linearLayout3"
    android:layout_weight="45">
    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="Username"
        android:background="@drawable/editor"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:textSize="20dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="#FFFF" />
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="Password"
        android:background="@drawable/editor"
        android:textSize="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="#FFFF" />
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerInParent="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/ProgressBarStyle"
        android:layout_gravity="center_horizontal"
        android:visibility="gone" />
    <Button
        android:text="Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnlogin"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:background="@drawable/buttonstyles"
        android:textColor="#F73494"
        android:layout_marginTop="20dp"
        android:textSize="12sp" />
</LinearLayout>

【问题讨论】:

    标签: android android-layout keyboard


    【解决方案1】:

    不要在代码中这样做。在 AndroidManifest xml 文件中执行此操作。在代码中执行此操作会得到奇怪的结果,尤其是在有对话框的情况下。

    该模式的作用是强制您的应用调整大小以超出键盘。但是 - 有些事情只是占用空间。如果您的布局有足够的空白来调整大小,它会。如果没有,那么键盘仍将覆盖应用程序的一部分。你没有发布你的布局,所以我不能给你更详细的答案。

    【讨论】:

    • 我已经更新了我原来的问题。请看一看。
    • 我注意到一件事——你的 weightsum 设置为 100,但所有权重的总和实际上是 105。删除顶部线性布局上的 weightsum,看看它是否可以解决问题。请注意,您仍然会遇到问题 - 看看“登录”一词的底部是如何被切断的。除了给它更多空间之外,没有办法解决这个问题。我认为这样做的唯一方法是缩小图像或缩小文本大小,没有太多垂直空白可以消除。
    • 哦,我没有意识到这一点。我将文本和图像做得尽可能小,但如果我把它们做得太小,它们在屏幕尺寸较大的设备上会显得太小。在不影响任何东西的情况下,使用所有这些组件实现该布局的最佳方式是什么。当键盘弹出时,Facebook 应用程序会隐藏登录按钮下方的所有内容,但他们仍然可以在小屏幕内管理所有内容。我没有那个有布局问题的设备,所以我必须稍后再测试。当键盘打开时,我们是否可以控制调整大小以及我们想要显示的内容?
    • 您拥有的最大控制权是减少整个页面的繁忙程度。 FB 在他们的登录页面上的内容很少——他们有很多垂直空白,但内容很少——一个徽标、两个文本框和一个按钮 IIRC。它的布局方式是,当键盘出现时,空格会减少并且一切都适合。
    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 2016-03-10
    • 2012-01-03
    • 2014-04-19
    • 2014-04-13
    • 1970-01-01
    • 2014-07-18
    • 2012-09-29
    相关资源
    最近更新 更多