SquareView没有正确布局它的孩子

时间:2018-03-25 12:07:06

标签: android layout

我有一个简单的布局,如下面的自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/someInnerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/iv_arrow"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/arrow"/>

</RelativeLayout>

这个功能在我的自定义视图中:

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (mForceSquareLayout) {
        int w = getMeasuredWidth();
        int h = getMeasuredHeight();
        if (w != h) {
            int size = Math.min(w, h);
            setMeasuredDimension(size, size);
        }
    }
}

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="?toolbar_view_theme"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?colorPrimary"
            android:elevation="16dp" />

        <CustomView
            android:id="@+id/wheel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="16dp"
            app:square_layout="true"
            app:arrow_image="@drawable/ic_arrow_drop_down_white_24dp"
            app:background_color="?colorPrimary" />

    </LinearLayout>
</layout>

问题是,如果方形逻辑确实调整了视图大小(它在横向模式下执行),则我的自定义视图子iv_arrow不再显示。看起来我的方形逻辑不会重新传播孩子们。任何想法如何解决这个问题?

我在视图和孩子身上尝试requestLayout但没有成功......

0 个答案:

没有答案