自定义视图缩小:以前占用的空间未清除

时间:2015-10-26 16:41:18

标签: android

在自定义视图中,OnMeasure和OnSizeChanged用于设置instrisic大小并分别获得最终大小。

在OnDraw中,使用当前的宽度/高度绘制一个drawable。

现在,如果告诉视图改变其内在大小,它将requestLayout。这会触发OnMeasure / OnSizeChanged,最后触发OnDraw。 它工作正常,可以使用正确的内部尺寸显示drawable。

问题是,如果新的内部尺寸小于旧内部尺寸,那么android会留下旧的图形(这似乎是""新的内部尺寸,但实际上应该已经被android清除了)。

我尝试清除OnDraw中的视图内容,但绘制的矩形已经被剪切到新的视图大小。

当孩子的视图大小缩小时,听起来像LinearLayout中的一个错误就是没有清除它的内容。在Android 4.4 /三星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>

更新自定义视图大小后看到的内容。只有&#34;黑色部分&#34;应该显示,而不是白色。

What i get

应该是这样的:

enter image description here

1 个答案:

答案 0 :(得分:0)

我发现了问题和解决方法。 真正的问题在于LinearLayout。从Android 4.0.3到Android 6.当此垂直linearlayout具有weight = 1和height = 0dp时,它只有一个子节点,其高度小于可用高度。这个孩子第一次被吸引,没问题。如果这个孩子的身高缩小,那么它下面的区域就不会重新粉刷。所以它保留了第一个孩子的旧内容,而不是重新填充布局背景颜色。

在下面的布局代码中,我演示了问题和解决方案。解决方案是添加第二个孩子,该孩子将占用第一个孩子下面的剩余自由空间。使用这种技术,当第一个孩子缩小时,它会按预期工作,第二个孩子会扩展,新的“空”空间会被正确重新绘制。

所以它确实是LinearLayout中的一个错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#CCD39F">
            <MyCustomView
                android:id="@+id/icon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <!-- Solution. To see the problem, remove the following layout -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#111111" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
        <Button
            android:text="Other demo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnNextImage" />
        <Button
            android:text="Next SVG"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnGoEmpty" />
    </LinearLayout>
</LinearLayout>