2016-12-24 7 views
0

なぜ私のスクロールビューでは起こらないのですか?編集テキストにフォーカスがない場合、レイアウトは下の図のようになります。ScrollViewはツールバーでカバーされています

enter image description here

が、ユーザーがエディットテキストボックスのいずれかであることを起こるとき、ここ

enter image description here

示すように、ツールバーやキーボードレイアウトが後ろにscrollviewを隠し、すべてのスペースを取りますこのためレイアウトは次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:id="@+id/activity_meal_viewer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/light_gray" 
    android:orientation="vertical"> 


    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

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

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/activity_horizontal_margin" 
      android:background="@android:color/white"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/activity_horizontal_margin"> 


       <EditText 
        android:id="@+id/person_name" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/activity_horizontal_margin" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_people" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_people" 
        android:gravity="center_vertical" 
        android:hint="@string/full_name" 
        android:inputType="textPersonName" 
        android:maxLines="1" 
        android:typeface="serif" /> 

       <EditText 
        android:id="@+id/person_address" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_pin" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_pin" 
        android:gravity="center_vertical" 
        android:hint="@string/address" 
        android:inputType="text" 
        android:maxLines="1" 
        android:typeface="serif" /> 

      </LinearLayout> 
     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/activity_horizontal_margin" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
      android:layout_marginTop="0dp" 
      android:background="@android:color/white" 
      app:cardCornerRadius="2dp"> 


      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/activity_horizontal_margin"> 

       <EditText 
        android:id="@+id/person_email" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/activity_horizontal_margin" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_email" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_email" 
        android:ems="10" 
        android:hint="@string/email" 
        android:inputType="textEmailAddress" 
        android:maxLines="1" 
        android:typeface="serif" /> 


       <EditText 
        android:id="@+id/person_phone" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_telephone" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_telephone" 
        android:ems="10" 
        android:hint="@string/phone" 
        android:inputType="phone" 
        android:typeface="serif" /> 
      </LinearLayout> 


     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/activity_horizontal_margin" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
      android:layout_marginTop="0dp" 
      android:background="@android:color/white" 
      app:cardCornerRadius="2dp"> 


      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/activity_horizontal_margin"> 


       <EditText 
        android:id="@+id/person_initial_fund" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/generic_shape_rect" 
        android:drawableLeft="@drawable/ic_dollar" 
        android:drawablePadding="@dimen/activity_horizontal_margin" 
        android:drawableStart="@drawable/ic_dollar" 
        android:ems="10" 
        android:hint="@string/initial_fund" 
        android:inputType="numberSigned" 
        android:maxLines="1" 
        android:typeface="serif" /> 
      </LinearLayout> 


     </android.support.v7.widget.CardView> 

     <ImageButton 
      android:layout_width="48dp" 
      android:layout_height="48dp" 
      android:layout_gravity="center" 
      android:layout_marginBottom="@dimen/activity_horizontal_margin" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
      android:layout_marginTop="0dp" 
      android:background="@drawable/selector_button_accent" 
      android:contentDescription="@string/done" 
      android:onClick="handlePeople" 
      android:src="@drawable/ic_action_done" /> 
    </LinearLayout> 
</LinearLayout> 

とツールバーのレイアウトファイルには、次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:fitsSystemWindows="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

注:私はまたNestesScrollViewを試してみましたが、それはうまくいきませんでしたどちらか

答えて

2

を読んfitsSystemWindowsを見る上で使用されている場合 、システムによって提供さWindowInsetsはパディングとしてViewによって消費され、ツールバーが上に余分なスペースを持っている理由です、システムがに、いくつかのtopPaddingを追加それは透明なステータスバーのためにWindowInsetsを受け取っているときです。

しかし、キーボードが表示されると、システムはキーボードの高さを下側のインセットとして新しいWindowInsetsをスローします。また、ツールバーがこの新しいWindowInsetsに反応すると、ツールバーはキーボードの高さを下の詰め物として追加し、スクリーンショットのように成長します。

この問題を回避するには、2つの方法があります。 まず、アクティビティのサイズを変更する代わりに、キーボードをアクティビティの内容に広げます。あなたは、あなたの活動のマニフェストエントリに設定することでこれを行うことができます:

<activity 
     android:name=".YourActivity" 
     android:windowSoftInputMode="adjustPan"/> 

これは、キーボードがアップしているときのEditTextがまだ表示されていることを確認し、キーボードがトップにすべてのコンテンツをプッシュするようになります。しかし、これは、キーボードがアップしているときにツールバーをウィンドウの外に押し出すことができます。それはあなたがそれで大丈夫かどうかを決定するあなたの呼び出しです。

もしあなたがいないのであれば、ツールバーに一番上のパディングを適用し、一番下のインセットを無視するように、WindowInsetを手動で処理するコードを書くことができます。 あなたのような何かを行うことができます。

ViewCompat.setOnApplyWindowInsetsListener(toolbar, new OnApplyWindowInsetsListener() { 
    @Override 
    public WindowInsetsCompat onApplyWindowInsets(
     View v, WindowInsetsCompat insets 
    ) { 
     v.setPadding(0, insets.getSystemWindowInsetTop(), 0, 0); 
     insets.consumeSystemWindowInsets(); 
     return insets; 
    } 
}); 

をしかし、このコードはコンパイルして任意のAPIレベル(それがサポート-V4からのものを使用しています)上で実行される場合でも、これが唯一のAPI 21日以降に有効となることに注意してください彼らがWindowInsetsの仕組みを変更したとき API19からtransparentStatusBarを使用している場合は、廃止予定のAPIを使用して同じことを行うためのコードを書く必要があります。 私はこれを無視し、transparentStatusBarをAPI 21から始めることができます。それは正直に言うと正当です。

希望はこれが役に立ちます。

1

はたぶん、あなたは、Googleのサポートの設計からCoordinatorLayout代わりのLinearLayoutを使用する必要があります。また、ツールバー内部のAppBarLayoutを追加します。ドキュメントは、デフォルトではthis

関連する問題