2016-06-17 9 views
2

Ich möchte PercentRelativeLayout während meiner Projekte verwenden, und dies erfolgreich mit Ansichten, die kein Bildlauf erfordern. Bei Verwendung von ScrollView's funktionieren jedoch Attribute wie layout_heightPercent und layout_marginTopPercent nicht.Wie Verwenden von PercentRelativeLayout in einem CollapsingToolbarLayout?

Mein Problem ist speziell, wenn ich eine CollapsingToolbarLayout und eine NestedScrollView verwende. Ich habe einige Hinweise gefunden, warum dies nicht wie gewünscht funktioniert here.

Eine solution (zweite Antwort) auf die Frage ist, die ScrollView zu erweitern und überschreiben die OnMeasure-Methode. Ich habe versucht, dies mit mehreren Ansichten zu machen, war aber nicht erfolgreich.

Hier ist der Plan Ich brauche Arbeit zu bekommen:

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

Jede Hilfe würde geschätzt.

Antwort

1

Ich vermasselte mit der code from here ein bisschen und fand eine Lösung. Jedes Mal, wenn Sie eine PercentRelativeLayout in einer Ansicht verwenden möchten, die Schriftrollen, Sie folgendes tun:

die Ansicht erweitern, außer Kraft setzen ihre onMeasure Methode, erhalten die gemessene Höhe und Breite und den zum setMeasuredDimension-Methode übergeben.

Wenn zum Beispiel ein AppBarLayout verwenden, um eine benutzerdefinierte Ansicht erstellen, wie folgt:

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); 
    } 
} 

Dann können Sie eine PercentRelativeLayout innerhalb der benutzerdefinierten Ansicht wie folgt verwenden:

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

Das gleiche Verfahren kann befolgt werden für NestedScrollView, ScrollView usw.