【问题标题】:How do you use a custom view in a HorizontalScrollView?如何在 Horizo​​ntalScrollView 中使用自定义视图?
【发布时间】:2013-10-16 17:38:10
【问题描述】:

我有一个自定义视图(称为CustomStrechView),其中extendsImageView。这个视图的目的是让我可以在不丢失纵横比的情况下获得一个图像来填充视图。

现在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);
    }
}

【问题讨论】:

  • 您遇到了什么错误??
  • @Hussain - 我没有收到“错误”,只是没有图像出现。 HorizontalScrollView 什么也没显示。启动画面正常加载并移动滚动视图,如果我使用ImageViews 然后我看到图片(但尺寸很差)如果我将它换成我的自定义视图然后什么都不会出现

标签: java android xml overloading android-imageview


【解决方案1】:

通过一些调试,我自己至少能够部分解决这个问题。原来MeasureSpec.getSize(widthMeasureSpec);调用得到的width为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);
}

【讨论】:

    猜你喜欢
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多