2015-10-26 15 views
0

在自定义视图中,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> 

更新自定义视图尺寸后,我看到了什么。只应显示“黑色部分”,而不是白色部分。

What i get

它应该是这样,而不是:

enter image description here

+0

你可以在不同的设备上试用吗?还可以尝试用'RelativeLayout'等其他布局替换'LinearLayout'。只是想隔离问题。此外,你应该显示“只有黑色部分”而不是白色部分。“ – Henry

回答

0

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

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

所以它实际上是LinearLayout中的一个bug。

<?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>