【问题标题】:Trying to use custom imageview (topcrop) in android, but the the app stops尝试在 android 中使用自定义 imageview (topcrop),但应用程序停止
【发布时间】:2015-09-02 17:11:06
【问题描述】:

我找到了以下帖子How set imageview scaletype to topCrop

基于此,我使用了来自 https://gist.github.com/arriolac/3843346 的 TopCropImageView 类,它扩展了 ImageView

以下是我的布局xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.simha.quotes.TopCropImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView23"
        android:src="@drawable/background" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="New Text"
        android:id="@+id/textView23"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="33dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
            android:textColor="#ffffff"
    android:textSize="14sp"
    android:textStyle="bold"
    android:shadowColor="#000000"
    android:shadowDx="4"
    android:shadowDy="4"
    android:shadowRadius="4" />
</RelativeLayout>

gradle 编译没有任何错误。但是当我使用该应用程序时,以及当我开始使用上述布局时。它不再说“不幸的是该应用程序停止了”。

是否有任何遗漏或方法错误。

【问题讨论】:

  • 用解决方案编辑

标签: android-custom-view


【解决方案1】:

我找到了解决方案:.java not using the 2- or 3-argument View constructors; XML attributes will not work 将这些添加到 TopCropImageView 的代码中,然后它就可以工作了。

public TopCropImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setScaleType(ScaleType.MATRIX);
}

public TopCropImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setScaleType(ScaleType.MATRIX);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多