4

我有一個RelativeLayout和一個EditText,它下面有Button。理想情況下,當用戶單擊EditText並填充多行文本時,RelativeLayout也會隨着行數的增加而擴大。然而,我現在的行爲是在EditText的第一行填滿之後,鍵盤開始覆蓋其下面的Button,並且在EditText之後的3-4行文本之後,Button不在鍵盤之下。有什麼辦法可以讓我在鍵入文本的時候,整個佈局的尺寸增大,使按鈕保持在鍵盤上方?這裏是我的代碼:軟鍵盤隱藏多行EditText下面的按鈕?

的AndroidManifest.xml:

<activity 
     android:name="<Name of project>.activity.Comments" 
     android:configChanges="orientation|keyboardHidden" 
     android:label="Comments" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar" 
     android:windowSoftInputMode="stateHidden|adjustResize" /> 

Comments.xml:

<RelativeLayout 
     android:id="@+id/send_message" 
     android:layout_width="match_parent" 
     android:layout_height="90dp" 
     android:layout_margin="4dp" 
     android:layout_alignParentBottom="true"> 

     <EditText 
      android:fontFamily="sans-serif" 
      android:id="@+id/write_comment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="12dp" 
      android:gravity="left" 
      android:textSize="16sp" 
      android:textColor="@color/black" 
      android:cursorVisible="false" 
      android:textColorHint="@color/material_color_grey_300" 
      android:hint="@string/commentBack" 
      android:background="@drawable/edittext_bg" 
      android:inputType="textMultiLine" 
      android:isScrollContainer="true" 
      android:maxLength="200" 
      android:scrollHorizontally="false" /> 

     <Button 
      android:fontFamily="sans-serif-medium" 
      android:id="@+id/send_comment" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" 
      android:textColor="@color/colorPrimary" 
      android:layout_marginTop="8dp" 
      android:text="@string/send" 
      android:background="@null" 
      android:layout_below="@id/write_comment" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentBottom="true" 
      android:textAllCaps="false" /> 
    </RelativeLayout> 

我已經嘗試添加android:isScrollContainer="true"以爲這樣就可以創建一個滾動容器的EditText如此這將有助於擴大布局,但似乎沒有工作。任何幫助,將不勝感激。謝謝!

編輯:已在整個佈局基於以下答案:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" android:fitsSystemWindows="true" 
    android:id="@+id/comments_layout"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/comments_appbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      android:layout_alignParentTop="true" 
      android:layout_marginTop="?attr/actionBarSize" 
      android:layout_width="match_parent" 
      android:id="@+id/comments_listview" 
      android:layout_height="match_parent"> 

      <ListView 
       android:scrollbars="vertical" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/rv_view_comments"> 
      </ListView> 

      <TextView 
       android:layout_below="@id/rv_view_comments" 
       android:gravity="center_horizontal" 
       android:padding="16dp" 
       android:textSize="16sp" 
       android:id="@+id/empty_list_item" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:visibility="gone" 
       android:text="@string/noComments" > 
      </TextView> 

      <include layout="@layout/include_progress_overlay"/> 

     </RelativeLayout> 

     <RelativeLayout 
    android:id="@+id/send_message" 
    android:layout_width="match_parent" 
    android:layout_height="90dp" 
    android:layout_margin="4dp" 
    android:layout_alignParentBottom="true"> 

    <EditText 
     android:fontFamily="sans-serif" 
     android:id="@+id/write_comment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="12dp" 
     android:gravity="left" 
     android:textSize="16sp" 
     android:textColor="@color/black" 
     android:cursorVisible="false" 
     android:textColorHint="@color/material_color_grey_300" 
     android:hint="@string/commentBack" 
     android:background="@drawable/edittext_bg" 
     android:inputType="textMultiLine" 
     android:isScrollContainer="true" 
     android:maxLength="200" 
     android:scrollHorizontally="false" /> 

    <Button 
     android:fontFamily="sans-serif-medium" 
     android:id="@+id/send_comment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="16sp" 
     android:textColor="@color/colorPrimary" 
     android:layout_marginTop="8dp" 
     android:text="@string/send" 
     android:background="@null" 
     android:layout_below="@id/write_comment" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" 
     android:textAllCaps="false" /> 
</RelativeLayout> 

    </RelativeLayout> 

</android.support.design.widget.CoordinatorLayout> 

編輯:按照Jaydroider的要求,我已經包括照片,以顯示我的問題。如果我可以做一個GIF什麼的,如果我增加更多的文字我EditText領域,Send按鈕將鍵盤下方消失,除非我從EditText刪除文本中,'發送不能推:

Image showing problem

編輯:我也展示瞭如何滾動到我的ListView的底部,當我點擊EditText這裏,如果這有幫助。

public void setupComment(final EditText comment) { 
     //Once we send the post, we want to scroll to bottom of listview 
     comment.setOnEditorActionListener(new TextView.OnEditorActionListener() { 

      @Override 
      public boolean onEditorAction(TextView v, int actionId, 
              KeyEvent event) { 
       if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { 
        comment.setImeOptions(EditorInfo.IME_ACTION_DONE); 
        resetKeyboardSettings(); 
       } 
       return false; 
      } 
     }); 

     //Hide the cursor until view is clicked on 
     View.OnTouchListener onTouchListener = new View.OnTouchListener(){ 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       System.out.println("Touched"); 
       if (v.getId() == comment.getId()) { 
        comment.setCursorVisible(true); 
       } 
       commentsView.postDelayed(new Runnable() { 
        @Override 
        public void run() { 

         commentsView.smoothScrollToPosition(commentsView.getAdapter().getCount()); 
        } 
       }, 750); 
       return false; 
      } 
     }; 
     comment.setOnTouchListener(onTouchListener); 
    } 
+0

當我檢查你的佈局時,我只能看到'List View'沒有任何其他的小部件。請詳細說明。 –

+0

@jaydroider我已經更新了上面的代碼來演示我的問題。希望有幫助。它是在編輯說'編輯:添加在整個佈局根據回答如下:' – user1871869

+0

是的,我已經檢查過我的佈局我需要澄清'編輯文本'和'按鈕'將顯示在哪裏。目前我只能看到'List View'。 –

回答

2

我已經嘗試過與您的Layout一切都完美,沒有問題。

But you need to make changes to your manifest where you declared your activity使用更多性能到達android:configChanges

android manifest.xml文件中執行此操作。

 <activity 
      android:name="<Name of project>.activity.Comments" 
      android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
      android:label="Comments" 
      android:screenOrientation="portrait" 
      android:theme="@style/AppTheme.NoActionBar" 
      android:windowSoftInputMode="stateHidden|adjustResize"/> 

下面是XML,我使用

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/comments_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.PopupOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/comments_appbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      android:id="@+id/comments_listview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/send_message" 
      android:layout_alignParentTop="true" 
      android:layout_marginTop="?attr/actionBarSize"> 

      <ListView 
       android:id="@+id/rv_view_comments" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_toEndOf="@+id/empty_list_item" 
       android:scrollbars="vertical" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

      <TextView 
       android:id="@+id/empty_list_item" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/rv_view_comments" 
       android:gravity="center_horizontal" 
       android:padding="16dp" 
       android:text="No Comments" 
       android:textSize="16sp" 
       android:visibility="gone" /> 


     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/send_message" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_margin="4dp"> 

      <EditText 
       android:id="@+id/write_comment" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#FFFFFF" 
       android:cursorVisible="false" 
       android:fontFamily="sans-serif" 
       android:gravity="left" 
       android:hint="Comment Back" 
       android:inputType="textMultiLine" 
       android:isScrollContainer="true" 
       android:maxLength="200" 
       android:padding="12dp" 
       android:scrollHorizontally="false" 
       android:textColor="#000000" 
       android:textColorHint="@android:color/holo_red_dark" 
       android:textSize="16sp" /> 

      <Button 
       android:id="@+id/send_comment" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_below="@id/write_comment" 
       android:layout_marginTop="8dp" 
       android:background="@null" 
       android:fontFamily="sans-serif-medium" 
       android:gravity="center|center_vertical" 
       android:text="Send" 
       android:textAllCaps="false" 
       android:textColor="@color/colorPrimary" 
       android:textSize="16sp" /> 
     </RelativeLayout> 

    </RelativeLayout> 

</android.support.design.widget.CoordinatorLayout> 

I have tested with above changes and it's just working fine you can see below.

enter image description here

希望這將幫助你解決你的問題。

+0

嘿,你介意發佈你的代碼,以便我可以模仿它嗎?我試圖聽取你在'Manifest'文件中的修改,但是這似乎並沒有爲我修復。 – user1871869

+0

@ user1871869你需要哪些代碼? –

+0

您用於創建上述圖像的XML佈局代碼? – user1871869

0

試試這個:

<RelativeLayout 
     android:id="@+id/send_message" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="4dp" 
     android:layout_alignParentBottom="true"> 

     <EditText 
      android:fontFamily="sans-serif" 
      android:id="@+id/write_comment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="12dp" 
      android:gravity="left" 
      android:textSize="16sp" 
      android:textColor="@color/black" 
      android:cursorVisible="false" 
      android:textColorHint="@color/material_color_grey_300" 
      android:hint="@string/commentBack" 
      android:background="@drawable/edittext_bg" 
      android:inputType="textMultiLine" 
      android:isScrollContainer="true" 
      android:maxLength="200" 
      android:scrollHorizontally="false" /> 

     <Button 
      android:fontFamily="sans-serif-medium" 
      android:id="@+id/send_comment" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" 
      android:textColor="@color/colorPrimary" 
      android:layout_marginTop="8dp" 
      android:text="@string/send" 
      android:background="@null" 
      android:layout_below="@id/write_comment" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentBottom="true" 
      android:textAllCaps="false" /> 
    </RelativeLayout> 
+0

嘿,我試着按照你的代碼。好像你剛剛在'RelativeLayout'標籤中加入了'android:layout_height = wrap_content'。我試圖實現你的建議,但似乎並沒有工作,現在'ListView'我已經覆蓋了我的'EditText'我發佈了我的整個佈局,希望它會更清楚,並可以幫助我解決這個問題。我試過你上面提到的,但是因爲'EditText'被隱藏了,所以我不知道如何解決它。 – user1871869

0

把你所有的編輯文本和按鈕滾動視圖。

<ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 
       <Edittext 
       <EditText 
       <EditText 
       <Btn 
      </LinearLayout 
     </scrollview 
0

我的解決辦法是增加ScrollView爲您佈局的根視圖和覆蓋查看的onSizeChanged方法。如果EditText的高度增加並且鍵盤正在阻止您的按鈕,請按Smooth Scroll

希望這會有所幫助。

+0

我怎麼會有一個'ScrollView'與嵌套'ListView'在它雖然?我明白你在說什麼,但我覺得這對我來說不起作用。 – user1871869