在自定义视图中,OnMeasure和OnSizeChanged用于设置内置大小并分别获取最终大小。自定义视图缩小:先前占用的空间不被清除
在OnDraw中,使用当前宽度/高度绘制drawable。
现在,如果视图被告知改变它的内在尺寸,它会请求Layout。这会触发OnMeasure/OnSizeChanged并最终触发OnDraw。 它工作正常,绘图总是使用正确的内部大小显示。
问题是,如果新的内部尺寸小于旧的尺寸,android离开旧的绘图(这似乎是“新”之下,但实际上应该已被android清除)。
我尝试清除OnDraw中的视图内容,但绘制矩形已被修剪为新的视图大小。
这听起来像是LinearLayout中的一个错误,当孩子的视图尺寸缩小时,它不会清除它的内容。在Android 4.4/Samsung S3 4G/CyanogenMod cm-11-20151004-NIGHTLY上进行测试。
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:padding="8dp">
<LinearLayout
android:orientation="horizontal"
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:gravity="top">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCD39F">
<MyCustomView
android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
更新自定义视图尺寸后,我看到了什么。只应显示“黑色部分”,而不是白色部分。
它应该是这样,而不是:
你可以在不同的设备上试用吗?还可以尝试用'RelativeLayout'等其他布局替换'LinearLayout'。只是想隔离问题。此外,你应该显示“只有黑色部分”而不是白色部分。“ – Henry