I would like to use PercentRelativeLayout
throughout my projects, and have been able to do so successfully with Views that don't require scrolling. When using ScrollView's
however, attributes such as layout_heightPercent
and layout_marginTopPercent
don't work.
My problem specifically is when I'm using a CollapsingToolbarLayout
and a NestedScrollView
. I have found some clues as to why this isn't working as desired here.
A solution (second answer) to the question is to extend the ScrollView
and override the onMeasure method. I tried doing this with several of the views but haven't been successful.
Here's the Layout I need to get working:
<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>
Any help would be appreciated.
I messed around with the code from here a bit and found a solution. Whenever you want to use a
PercentRelativeLayout
inside a view that scrolls, you can do the following:Extend the view, override its
onMeasure
method, get the measured height and width and pass those to thesetMeasuredDimension
method.For example, when using an
AppBarLayout
, create a custom view as follows:Then you can use a
PercentRelativeLayout
inside the custom view as follows:The same procedure can be followed for
NestedScrollView
,ScrollView
, etc.