2016-11-08 12 views
0

私はPercentRelativeLayoutを拡張するカスタムViewを作成し、Viewを強制的に2次式にしました。このビューの子要素は、親のサイズ(高さと幅)と一致する必要があります。子供のlayout_widthまたはlayout_heightmatch_parentまたはfill_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); 
} 
関連する問題