2016-06-17 10 views
2

私のプロジェクト全体でPercentRelativeLayoutを使いたいですし、スクロールを必要としないビューでもそれを成功させることができました。ただし、ScrollView'sを使用する場合、layout_heightPercentlayout_marginTopPercentなどの属性は機能しません。CollapsingToolbarLayoutでPercentRelativeLayoutを使用するには?

私の問題は、具体的には私がCollapsingToolbarLayoutNestedScrollViewを使用しているときです。私はこれが望むように動作していない理由のいくつかの手がかりを見つけましたhere

solution(2番目の回答)は、ScrollViewを拡張してonMeasureメソッドをオーバーライドすることです。私はいくつかのビューでこれをやってみましたが、成功しませんでした。任意の助けをいただければ幸いです

<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/sign_in_coordinator_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#000000" 
android:fitsSystemWindows="true" 
android:animateLayoutChanges="true"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/sign_in_app_bar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true" 
    android:animateLayoutChanges="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/sign_in_collapsing_toolbar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     android:animateLayoutChanges="true" 
     app:expandedTitleMarginStart="48dp" 
     app:expandedTitleMarginEnd="64dp" 
     app:contentScrim="@color/primaryBackground" 
     app:layout_scrollFlags="exitUntilCollapsed|scroll" 
     > 

     <android.support.percent.PercentRelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="0dp" 

       app:layout_widthPercent="50%" 
       app:layout_heightPercent="80%" 
       app:layout_marginLeftPercent="33%" 

       android:scaleType="centerCrop" 
       android:src="@drawable/picture" 
       android:fitsSystemWindows="true" 
       app:layout_collapseMode="parallax"/> 

     </android.support.percent.PercentRelativeLayout> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="58dp" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

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

<android.support.v4.widget.NestedScrollView 
    android:id="@+id/scroll_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clipToPadding="false" 
    android:animateLayoutChanges="true" 
    app:behavior_overlapTop="315dp" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.percent.PercentRelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="0dp" 

      app:layout_widthPercent="50%" 
      app:layout_heightPercent="80%" 
      app:layout_marginLeftPercent="33%" 

      android:scaleType="centerCrop" 
      android:src="@drawable/picture" 
      android:fitsSystemWindows="true" /> 

    </android.support.percent.PercentRelativeLayout> 
</android.support.v4.widget.NestedScrollView> 

は、ここで私が働いて取得する必要がありますレイアウトです。

答えて

1

私はcode from hereで少しばかり解り、解決策を見つけました。あなたがスクロールし、次の操作を行うことができますビュー内PercentRelativeLayoutを使用したいとき:

は、ビューを拡張してそのonMeasureメソッドをオーバーライドして、測定した高さと幅を取得し、setMeasuredDimension方法にそれらを渡します。

public class MyAppBarLayout extends AppBarLayout { 

    public MyAppBarLayout(Context context) { 
     super(context); 
    } 

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

    // For some scrolling views, this method isn't defined, so just comment it out 
    public MyAppBarLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = getMeasuredWidth(); 
     int height = getMeasuredHeight(); 

     setMeasuredDimension(width, height); 
    } 
} 

次のように続いて、カスタムビュー内PercentRelativeLayoutを使用することができます:

<com.mydomain.myapp.MyAppBarLayout   
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 

    android:id="@+id/appBarLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.percent.PercentRelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ImageView 
      app:layout_widthPercent="50%" 
      app:layout_heightPercent="50%" 
      app:layout_marginTopPercent="25%" 
      app:layout_marginLeftPercent="25%"/> 

    </android.support.percent.PercentFrameLayout>  
</com.mydomain.myapp.MyAppBarLayout> 

同じ手順をすることができ、次のようにカスタムビューを作成し、AppBarLayoutを使用して例えば

NestedScrollView,ScrollViewなどに従ってください。

関連する問題