【问题标题】:background is scrolling it should be constant?背景正在滚动它应该是恒定的吗?
【发布时间】:2015-08-21 18:39:17
【问题描述】:

我设计的登录布局如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bg">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/login_fields_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="20dp">

            <ImageView
                android:id="@+id/loginLogo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingTop="30dp"
                android:src="@drawable/logo" />

            <EditText
                android:id="@+id/userNameEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="80dp"
                android:background="@drawable/textfield"
                android:drawableLeft="@drawable/username"
                android:drawablePadding="10dip"
                android:hint="@string/hint_username"
                android:imeOptions="actionNext"
                android:inputType="textEmailAddress"
                android:maxLength="50"
                android:padding="10dp"
                android:singleLine="true"
                android:textColor="@color/login_textcolor"
                android:textCursorDrawable="@null" />

            <EditText
                android:id="@+id/passwordEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/textfield"
                android:drawableLeft="@drawable/password"
                android:drawablePadding="10dip"
                android:hint="@string/hint_password"
                android:imeOptions="actionNext"
                android:inputType="textPassword"
                android:padding="10dp"
                android:singleLine="true"
                android:textColor="@color/login_textcolor"
                android:textCursorDrawable="@null" />


            <LinearLayout
                android:id="@+id/loginOptionsLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="2">

                <CheckBox
                    android:id="@+id/rememberMeCheckBox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:button="@null"
                    android:checked="false"
                    android:drawableEnd="@drawable/bg_checkbox"
                    android:gravity="center_vertical"
                    android:paddingRight="40dp"
                    android:text="@string/text_rememberme"
                    android:textColor="@color/login_textcolor"
                    android:textSize="14sp" />

                <Button
                    android:id="@+id/forgotPasswordButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@null"
                    android:gravity="right|center_vertical"
                    android:singleLine="true"
                    android:text="@string/text_forgotpassword"
                    android:textColor="@color/login_textcolor"
                    android:textSize="14sp" />

            </LinearLayout>

            <Button
                android:id="@+id/loginButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:background="@drawable/button_bg"
                android:text="@string/text_login"
                android:textColor="@android:color/white"
                android:textSize="20dp"
                android:textStyle="bold" />`
        </LinearLayout>


    </ScrollView>

</RelativeLayout>

当我触摸edittext时,背景向上移动它需要保持不变,因为它在滚动视图之外。如何解决这个问题。

我提到了以下内容 Background Image Placement 因为他们需要在底角我需要它作为背景。但无论如何我好奇地尝试了它,但它仍然在滚动背景

【问题讨论】:

  • @nkorth 位于左下角。在我的场景中,它是整个背景。
  • 是的,您可以使用相同的技术将整个背景锚定到顶部边缘。我认为您的情况是:默认情况下背景锚定在中心,这意味着当视口缩小时(由于键盘),背景似乎会移动。如果背景固定在顶部,则高度变化不会移动背景。
  • 我也试过了,它不工作它正在将背景更多地放在顶部。它不应该向上移动背景应该是恒定的。
  • @nkorth 你能试试这个代码,然后按上下箭头。 annitha Manikandan 给出的答案是有效的。

标签: android layout android-edittext scrollview android-relativelayout


【解决方案1】:

我试过了,效果很好。从 xml 布局中删除背景

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getWindow().setBackgroundDrawableResource(R.drawable.login_bg) ;

【讨论】:

  • 您能解释一下为什么需要更改 SoftInputMode 吗?没有解释的代码对于以后发现这个问题的人来说不是一个有用的答案,现在甚至可能也不是一个有用的答案。
  • softInputMode 很重要,因为当我们单击 EditText 时,键盘会打开。背景变化是由于窗口大小和位置的变化。所以我们正在为窗口本身设置背景并控制窗口屏幕大小的变化。
  • 不调整屏幕大小可能会产生其他副作用,包括对用户隐藏内容。设置SOFT_INPUT_STATE_ALWAYS_HIDDEN 似乎是一个更糟糕的主意。
  • 在某些布局中,这将是解决方案。特别是对于这种布局,我可以上下滚动,内容都显示得很好!
  • 这对我来说很好用。谢谢@AnithaManikandan。
【解决方案2】:

在您的活动中使用以下内容

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

【讨论】:

  • 我尝试了上面的语句,如果我使用它屏幕不滚动。背景不应该滚动。但是屏幕应该滚动。
猜你喜欢
  • 1970-01-01
  • 2014-10-30
  • 2014-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多