2016-11-08 96 views
0

我做了一个自定义视图,它扩展了PercentRelativeLayout并强制视图是二次的。这个视图的子元素应该匹配他们父母的大小(高度和宽度)。如果我将孩子的layout_widthlayout_height设置为match_parentfill_parent,它们会覆盖父视图(如您在图片的右半部分所看到的)。子元素在自定义布局外不可见,但我无法通过相对大小正确对齐子元素。子元素溢出自定义RelativeLayout

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> 

这对儿童浏览标准的行为吗?我怎样才能改变它使孩子符合父母的大小?

回答

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