2015-09-18 16 views
9

FrameLayout(引き出しレイアウトのコンテナ)に問題があります。 FrameLayoutの高さが画面の高さを超えています(下部のアンドロイドのデフォルトメニューボタンの下)。Android - フレームレイアウトの高さがコーディネーターレイアウトと一致しません

<android.support.design.widget.CoordinatorLayout 
     android:id="@+id/main_content" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/navContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:minHeight="?attr/actionBarSize" 
       app:layout_scrollFlags="scroll|enterAlways" /> 

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

     <FrameLayout 
      android:id="@+id/container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

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

Height exceeding as seen in Android Studio Designer

+1

この問題は、このアンドロイドコーディネータのバグですか、これに対する回避策ですか? (アクションバーの高さの2乗としてマージンの底を与えないで) – DeepakPanwar

答えて

4

私の最初の試みはでframeLayoutでandroid:layout_marginBottom="?attr/actionBarSize"を設定することでした。これにより、垂直スクロール可能なコンテンツがない(通常のRelativeLayoutのように、match_parentの高さの)高さが「固定」の非スクロール可能ビューの解決策が解決されました。親ボトム(android:layout_alignParentBottom="true")にコンポーネントを配置すると、可視の要素が表示されます。 Android Studioのプレビューアでは、高さを超えることはありません。

しかし、このmarginBotton-fixは、(RecyclerViewのように)ルートビューがスクロール可能なフラグメントに新しい問題をもたらします。下のマージンを下にスクロールすると、白いバー(白が背景色の場合)に表示されます。 DR私は非スクロール可能な断片に?attr/actionBarSizeとして下の余白を適用することによって、その問題を中心に働いていた内部示され、それらのビューのためにネストされたスクロール機能はツールバー ListView with last item cut

TLを引き出しますように、これは、合理的な縫い目Framelayout。その前に私はツールバーの高さを?attr/actionBarSizeに設定しました。

活動のレイアウト:

 <android.support.design.widget.AppBarLayout 
      android:id="@+id/navContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_scrollFlags="scroll|enterAlways" /> 

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

     <FrameLayout 
       android:id="@+id/container" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     /> 

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

フラグメントレイアウト:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="?attr/actionBarSize" 
      android:orientation="vertical"> 
      <!-- Further stuff here --> 
      <TextView android:id="@+id/label" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
      /> 
    </LinearLayout> 

私が今直面している唯一の欠点は、フラグメントレイアウトを作成中のAndroid Studioのプレビューアに示される空白です。

+0

これは私のためにうまくいった!副題として、フラグメントの1つにアプリケーションバーを隠すスクロール可能なルートがある場合、以前の(スクロール不可能な)フラグメントに戻って再び表示する方法がない場合は、隠れたままになります。この問題の回避策があります。[ここをクリックしてください](http://stackoverflow.com/q/30554824/1794631)。 – rmorrin

1

の中に別のFragmentsを使用すると、Fragmentsにはスクロール可能なコンテンツがあり、スクロールしないものがあります。 Toolbarにはスクロールフラグ "scroll|enterAlways"があります。これは前のレイアウトではOKですが、後者ではOKではありません。私の解決策は、カスタムタグ(contentShouldNotScrollTag)に依存するスクロールフラグを切り替えるカスタムAppBarLayout.Behaviorです。レイアウトのためにこのタグを設定し、このようにスクロールするべきでない。その結果

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:tag="@string/contentShouldNotScrollTag"> 
    <!-- my non-scrollable Fragment layout --> 
</FrameLayout> 

、この断片の高さが画面の高さを超えることはありません。 AppBarLayoutのカスタムビヘイビアクラスは次のとおりです。

public class MyScrollBehavior extends AppBarLayout.Behavior { 
    private View content; 

    public MyScrollBehavior(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onMeasureChild(CoordinatorLayout parent, AppBarLayout appBarLayout, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) { 
     if(content == null) { 
      content = parent.findViewById(R.id.container); 
     } 

     if(content != null) { 
      boolean shouldNotScroll = content.findViewWithTag(parent.getContext().getString(R.string.contentShouldNotScrollTag)) != null; 
      Toolbar toolbar = (Toolbar) appBarLayout.findViewById(R.id.toolbar); 
      AppBarLayout.LayoutParams params = 
        (AppBarLayout.LayoutParams) toolbar.getLayoutParams(); 
      if (shouldNotScroll) { 
       params.setScrollFlags(0); 
       appBarLayout.setExpanded(true, true); 
      } else { 
       params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL 
         | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); 
      } 
     } 

     return super.onMeasureChild(parent, appBarLayout, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed); 
    } 
} 
関連する問題