2017-09-22 83 views
0

我遇到了windowSoftInputMode="adjustResize"setVisibility(View.VISIBLE)的問題。 adjustResize按預期工作,直到View的可見性發生變化。windowSoftInputMode =「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> 

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

有什麼辦法可以解決這個意外的行爲嗎?

回答

0

我也面臨同樣的問題,並沒有找到任何解決方案,如果它緊急然後暫時你可以申請這個。

每當您更改視圖的可見性時,都會以編程方式調整您的佈局的大小。

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

希望這對你有所幫助。

+0

我會試一試 –

+0

我試了一下,它表現得很時髦。第一次當可見性被改變時,它破壞了佈局,當添加更多文本時,它回到正常佈局。 我在'mNextButton.setVisibility(View.VISIBLE);'和'mNextButton.setVisibility(View.GONE)'後面添加了你的建議;'在我的Fragmant.java上 –

相關問題