0
我做了一個自定義視圖,它擴展了PercentRelativeLayout
並強制視圖是二次的。這個視圖的子元素應該匹配他們父母的大小(高度和寬度)。如果我將孩子的layout_width
或layout_height
設置爲match_parent
或fill_parent
,它們會覆蓋父視圖(如您在圖片的右半部分所看到的)。子元素在自定義佈局外不可見,但我無法通過相對大小正確對齊子元素。子元素溢出自定義RelativeLayout
該自定義視圖:
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>
這對兒童瀏覽標準的行爲嗎?我怎樣才能改變它使孩子符合父母的大小?