子元素溢出自定义RelativeLayout

时间:2016-11-08 19:13:48

标签: android android-layout android-view

我制作了一个自定义视图,扩展了PercentRelativeLayout并强制视图为二次方。此视图的子元素应与其父级的大小(高度和宽度)匹配。如果我将子项layout_widthlayout_height设置为match_parentfill_parent,则会将其扩展到父视图上(如右图所示) )。子元素在自定义布局之外不可见,但我无法通过相对大小正确对齐子项。

Children extending over parent

自定义视图:

public class QuadraticPercentRelativeLayout extends PercentRelativeLayout {

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

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

    public QuadraticPercentRelativeLayout(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();
        int size = width < height ? width : height;
        setMeasuredDimension(size, size);
    }
}

这是我的布局(简化):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin">

    <QuadraticPercentRelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/progress_bar">

        <ImageView
            android:id="@+id/page_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/package_stock"/>

        <ImageView
            android:id="@+id/next_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/page_view"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            app:layout_widthPercent="35%"
            app:srcCompat="@color/ColorPrimaryDark"/>

        <ImageView
            android:id="@+id/previous_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/page_view"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            app:layout_widthPercent="35%"
            app:srcCompat="@color/ColorPrimaryDark"/>

    </QuadraticPercentRelativeLayout>

</RelativeLayout>

这是儿童观看的标准行为吗?如何更改它以使子项符合父项大小?

1 个答案:

答案 0 :(得分:0)

我修复了QuadraticPercentRelativeLayout类中的onMeasure方法:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int size = Math.min(width, height);
    int measureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
    super.onMeasure(measureSpec, measureSpec);
}