我想在我的整个项目中使用 PercentRelativeLayout
,并且已经能够使用不需要滚动的 View 成功地做到这一点。但是,当使用 ScrollView 的
时,layout_heightPercent
和 layout_marginTopPercent
等属性不起作用。
我的问题特别是在我使用 CollapsingToolbarLayout
和 NestedScrollView
时。我发现了一些线索,说明为什么这不能按预期工作 here .
A solution问题的(第二个答案)是扩展 ScrollView
并覆盖 onMeasure 方法。我尝试用几个 View 来做这件事,但没有成功。
这是我需要开始工作的布局:
<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>
如有任何帮助,我们将不胜感激。
最佳答案
我弄乱了 code from here有点,找到了解决办法。每当您想在 ScrollView 中使用 PercentRelativeLayout
时,您可以执行以下操作:
扩展 View ,覆盖其onMeasure
方法,获取测量的高度和宽度并将它们传递给setMeasuredDimension
方法。
例如,当使用 AppBarLayout
时,创建一个自定义 View 如下:
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);
}
}
然后您可以在自定义 View 中使用 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>
可以对 NestedScrollView
、ScrollView
等执行相同的过程
关于android - 如何在 CollapsingToolbarLayout 中使用 PercentRelativeLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37888749/