【问题标题】:layout does not move up even after adjustPan,adjustResize即使在adjustPan,adjustResize之后布局也不会向上移动
【发布时间】:2017-05-17 16:10:53
【问题描述】:

我的代码如下图:

AndroidManifest.xml

<activity
        android:name=".activity.LogInActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="stateHidden|adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

activity_login.xml

    <?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/ic_dashback"
   >


    <RelativeLayout
        android:id="@+id/card_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        >

        <RelativeLayout
            android:layout_width="235dp"
            android:layout_height="264dp"
            android:background="@color/colorMobster7B"
            android:fitsSystemWindows="true"
            android:orientation="vertical"
            android:paddingLeft="20dp"
            android:paddingRight="20dp">


            <EditText
                android:id="@+id/userName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:background="@drawable/background_edittext_login"
                android:hint="Email"
                android:maxLines="1"
                android:minLines="1"
                android:paddingBottom="5dp"
                android:singleLine="true"
                android:textColor="@color/colorBlackCurrent"
                android:textColorHint="@color/colorBlackCurrent"
                android:textSize="13sp" />


            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userName"
                android:layout_marginTop="20dp"
                android:background="@drawable/background_edittext_login"
                android:hint="Password"
                android:inputType="textPassword"
                android:maxLines="1"
                android:minLines="1"
                android:paddingBottom="5dp"
                android:singleLine="true"
                android:textColor="@color/colorBlackCurrent"
                android:textColorHint="@color/colorBlackCurrent"
                android:textSize="13sp" />


            <TextView
                android:id="@+id/forgotPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/password"
                android:layout_marginTop="13dp"
                android:text="Forgot Password"
                android:textSize="13sp" />


            <Button
                android:id="@+id/loginButton"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_below="@+id/forgotPassword"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="27dp"
                android:background="@color/colorBlackCurrent"
                android:gravity="center_vertical|center"
                android:text="LOGIN"
                android:textAllCaps="true"
                android:textColor="@color/colorMobster7B"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/createAccount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/loginButton"
                android:layout_gravity="center"
                android:text="Create account"
                android:textAllCaps="true"
                android:textSize="17sp" />
        </RelativeLayout>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_view"
        android:layout_centerInParent="true"
        android:layout_marginTop="18dp"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/bottomText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Powered by"
            android:textAllCaps="true"
            android:textColor="@color/colorMobster7B"
            android:textSize="12sp" />

        <ImageView
            android:layout_width="55dp"
            android:layout_height="19dp"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/bottomText"
            android:background="@drawable/ic_quflip" />
    </RelativeLayout>

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="105dp"
        android:visibility="invisible" />


</RelativeLayout>

LoginActivity.java

public class LogInActivity extends AppCompatActivity {
.....
 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
.....
}
}

现在,我的布局是针对平板电脑的。我想要的是,一旦键盘打开,整个布局应该向上移动,而不是特定编辑文本的区域。 我已经尝试过“adjustPan”、“adjustResize”、“adjustPan|adjustResize”,并且我已经将每个布局定义为 relativelayout。但是没有任何效果,那我应该怎么做才能让它工作呢?

【问题讨论】:

  • 尝试在relativelayout中添加fitsystemWindow=true
  • 我应该把 fitsystemWindow=true 放在哪个相对布局中?我应该使用什么 windowsSoftInputMode 标志?
  • stateHidden|adjustResize 和最上面的父级关系
  • 不,它不会产生将整个布局向上推的效果。为此,我在上面的帖子中更新了我的代码。如果我没有完全按照你的建议做,请告诉我。
  • 尝试一件事。将您的第二个 REelative 布局放入 Scrollview。

标签: android android-layout


【解决方案1】:

Manifest 内部定义android:windowSoftInputMode 如下。

android:windowSoftInputMode="stateHidden|adjustResize"

Activityprogrammatically 里面也这样做。

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

编辑

也可以在 Manifest 中添加这个并尝试。

android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"

我已经使用您的layout 测试了输出。

以下是平板电脑的输出。

【讨论】:

  • 我已经按照你展示的方式完成了,但它不适合我
  • @MrugeshThaker 检查并尝试编辑一个。试试你的运气。如您所见,这一切都对我有用。
  • 我在帖子中更新了我的代码,你能告诉我我和你的代码是一样的吗?另外我把 screenorientation=landscape
  • @MrugeshThaker 代码相同。
  • 但在我的设备中效果不一样
猜你喜欢
  • 2019-06-29
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多