我有一个自定义 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。我>认为这是因为在HorizontalScrollView
中,宽度不确定(取决于其中的内容),因此无论MeasureSpec.getSize(widthMeasureSpec);
实际上所做的是与父级对话,并且普通 Linear
或 RelativeLayout
中的父级具有定义的宽度,就像在 HorizontalScrollView
中一样事实并非如此……但这只是目前的猜测。
所以我对此进行了修复,如果宽度
为0,我们将通过高度
来运行计算。 height
需要两种情况来覆盖它,以避免除以 0 错误(else if
和 else
统称)。
这是我当前的解决方案,其中包含我在问题中指出的相同 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 - 如何在 HorizontalScrollView 中使用自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19410114/