【问题标题】:windowSoftInputMode=“adjustResize” not working after changing visibility of childwindowSoftInputMode=“adjustResize”在改变孩子的可见性后不起作用
【发布时间】:2018-03-03 12:49:55
【问题描述】:

我遇到了windowSoftInputMode="adjustResize"setVisibility(View.VISIBLE) 的问题。在 View 的可见性发生更改之前,adjustResize 一直按预期工作。

因此,只要我不以编程方式更改任何内容的可见性,adjustResize 就会按预期工作,但一旦更改任何内容的可见性,它就会停止调整布局大小。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ABC">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".view.activity.MainActivity"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="Main_ACTIVITY" />

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

    </application>

</manifest>

片段.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="ABC.view.fragment.Fragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.2"
            android:gravity="center">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/circleImg"
                style="@style/AppTheme.CircleImgList"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.8"/>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/relativeLayout">

        <EditText
            android:id="@+id/editName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:textColor="@color/white"
            android:hint="Enter Your Name"
            android:textColorHint="#AAFFFFFF"
            android:textSize="25sp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginRight="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:inputType="textCapWords"
            android:maxLines="1"
            android:layout_gravity="start"
            android:layout_centerVertical="true"/>

        <TextView
            android:id="@+id/nextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NEXT"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:textSize="20sp"
            android:textColor="@color/white"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:clickable="true"
            android:visibility="gone"/>

    </RelativeLayout>

</RelativeLayout>

和 Fragment.java 的onViewCreated

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    mCircleImgView = view.findViewById(R.id.circleImg);
    mEditName = view.findViewById(R.id.editName);
    mNextButton = view.findViewById((R.id.nextButton));

    mCircleImgView.setImageURI(mImageURI);

    mEditName.requestFocus();
    InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imgr.showSoftInput(mEditName, InputMethodManager.SHOW_IMPLICIT);

    //Change visibility of mNextButton
    mEditName.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {}

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String text = mEditName.getText().toString();
            if (text.length() >= 1) {
                mNextButton.setVisibility(View.VISIBLE);
            }
            else {
                mNextButton.setVisibility(View.GONE);
            }
        }
    });
}

如何在视图可见性更改后立即停止调整布局大小的 Gif: https://i.stack.imgur.com/WH9dn.gif

有什么办法可以解决这种意外行为吗?

【问题讨论】:

    标签: android android-layout resize window-soft-input-mode


    【解决方案1】:

    我也遇到了同样的问题,没有找到任何解决方案,如果它是紧急的,那么你可以申请这个。

    每当您要更改视图的可见性时,使用此程序以编程方式调整布局大小。

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

    希望对您有所帮助。

    【讨论】:

    • 我会试试的
    • 我试了一下,它的行为很时髦。第一次更改可见性时,它会破坏布局,并在添加更多文本时恢复正常布局。我在我的 Fragmant.java 上的 mNextButton.setVisibility(View.VISIBLE);mNextButton.setVisibility(View.GONE); 之后添加了您的建议
    【解决方案2】:

    检查您是否处于全屏模式或主题中的 translucentStatus 设置为 true,这会标记全屏模式。这会使 adjustResize 功能失效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      相关资源
      最近更新 更多