如何在Horizo​​ntalScrollView中使用自定义视图?

时间:2013-10-16 17:38:11

标签: java android xml overloading android-imageview

我有CustomStrechView extends的自定义视图(名为ImageView)。这个视图的重点是,我可以在不丢失宽高比的情况下获取图像来填充视图。

现在CustomStrechView在大多数应用程序中使用它时工作正常,但当我尝试在HorizontalScrollView中应用它时,图像似乎都消失了。我不确定为什么会这样。我已经尝试删除<HorizontalScrollView />,当我将图像显示回来时,我觉得有一些属性需要设置,但我似乎无法找到它。

任何人都可以指出我做错了什么或更好的方式让自定义视图在HorizontalScrollView中工作吗?


修改

在玩了一下之后,我已经确定只有在我对width维度进行硬编码时才会显示自定义视图,即:

<com.example.dragdropshapes.CustomStrechView
    android:layout_width="200dp"

将高度保留为fill_parent很好......


代码

以下是我在xml文件中使用自定义视图的方法:

<!-- Removing this makes the images show up -->
<HorizontalScrollView       
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:layout_marginTop="50dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:showDividers="middle"
        android:orientation="horizontal">

        <!-- Changing this to a normal ImageView makes the image show up -->
        <com.example.dragdropshapes.CustomStrechView
            android:id="@+id/pickshapes"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:clickable="true"
            android:onClick="pickShapes"
            android:background="@drawable/border"
            android:src="@drawable/ic_launcher"
            />
        <!-- There are 5 other custom ImageViews here -->
    </LinearLayout>
</HorizontalScrollView>

以下是自定义视图代码的主要部分:

public class CustomStrechView extends ImageView{

  private final Drawable srcPic;
  private final String TAG = "strechyTag";
  private boolean changeSize = false;
  private int Xpx;
  private int Ypx;

    public CustomStrechView(Context context) {
        super(context);
        srcPic = this.getDrawable();
    }

     //... other constructors, not really important...

     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
         Log.d(TAG, "in the onMeasure!!\n");
         int width, height;

        width = MeasureSpec.getSize(widthMeasureSpec);
        height = width * srcPic.getIntrinsicHeight() / srcPic.getIntrinsicWidth();
        setMeasuredDimension(width, height);
    }
}

1 个答案:

答案 0 :(得分:0)

通过一些调试,我能够至少部分解决这个问题。事实证明,从width调用获得的MeasureSpec.getSize(widthMeasureSpec);为0.因此,计算以宽度和高度结束为0,setMeasuredDimension调用将使图像不可见。

现在我说我已经解决了部分,因为我仍然不明白为什么我得到的值是0.我 想想 ,因为在HorizontalScrollView中宽度未确定(取决于其中的内容),因此MeasureSpec.getSize(widthMeasureSpec);实际正在做的是与父母交谈,以及正常LinearRelativeLayout中的父级具有已定义的宽度,而HorizontalScrollView中的父级则没有......但这只是猜测。

所以我为它做了一个修正,如果width为0,我们通过height进行计算。 height需要两个案例来覆盖它以避免除以0错误(else ifelse集体)。

这是我当前的解决方案,其中包含我在问题中提到的相同xml文件。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    if(width > 0)
        height = width * srcPic.getIntrinsicHeight() / srcPic.getIntrinsicWidth();
    else if(srcPic.getIntrinsicWidth() < srcPic.getIntrinsicHeight())
     width = height / (srcPic.getIntrinsicHeight() / srcPic.getIntrinsicWidth());
    else
     width = height / (srcPic.getIntrinsicWidth() / srcPic.getIntrinsicHeight());

    setMeasuredDimension(width, height);
}
相关问题