java - 如何在 Horizo​​ntalScrollView 中使用自定义 View ?

标签 java android xml overloading android-imageview

我有一个自定义 View (称为 CustomStrechView ),其中 extends一个ImageView 。这个 View 的目的是让我可以获得一个图像来填充 View 而不丢失纵横比。

现在是CustomStrechView当我在大多数应用程序中使用它时效果很好,但是当我尝试在我的 HorizontalScrollView 中应用它时,图像似乎全部消失了。我不知道为什么会发生这种情况。我尝试删除 <HorizontalScrollView />当我做图像显示备份时,所以我觉得有一些属性需要设置,但我似乎找不到它。

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

<小时/>

编辑:

经过一番尝试后,我确定只有在对 width 进行硬编码时,我才能获得自定义 View 来显示图像。维度,即:

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

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

<小时/>

代码:

以下是我的自定义 View 在 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>

这是自定义 View 代码的主要部分:

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

最佳答案

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

现在我说我已经部分解决了这个问题,因为我仍然不明白为什么我得到的值是0。我>认为这是因为在Horizo​​ntalScrollView中,宽度不确定(取决于其中的内容),因此无论MeasureSpec.getSize(widthMeasureSpec); 实际上所做的是与父级对话,并且普通 LinearRelativeLayout 中的父级具有定义的宽度,就像在 Horizo​​ntalScrollView 中一样事实并非如此……但这只是目前的猜测。

所以我对此进行了修复,如果宽度为0,我们将通过高度来运行计算。 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);
}

关于java - 如何在 Horizo​​ntalScrollView 中使用自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19410114/

相关文章:

c# - 如何在C#中编写此Java代码

java - 线程创建和同步所需的时间

java - Android Parcelable final

c++ - 克隆rapidxml::xml_document

xml - XSLT - 重命名节点并删除空格等等

java - 尝试在控制台中运行 java 程序将给出无法找到或加载主类错误

java - Quarkus 1.3 无法实例化 SAXParserFactory

android - Admob 广告类型是否存在收入不平等

android - 图片不显示在图库中

php - 您如何在PHP中解析和处理HTML/XML?