【问题标题】:How to remove any xml property from java code in Android如何从Android中的java代码中删除任何xml属性
【发布时间】:2017-04-05 11:00:28
【问题描述】:

我已经设置了ImageView 的这些属性

<ImageView
    android:id="@+id/image_view"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHorizontal_bias="0.0"
    android:adjustViewBounds="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    app:layout_constraintBottom_toBottomOf="parent"
    android:scaleType="centerCrop"
    />

现在从 Java 代码中,我想从这个图像视图中删除 scaleType 属性。我不希望此 ImageView 上有任何比例类型。

有可能实现吗?

【问题讨论】:

  • 尝试删除android:scaleType="centerCrop" 并重新检查?
  • 你检查the docs了吗?
  • 您的意思是,您希望 ImageView 的原始意图具有 scaleType 但您想在运行时更改它?
  • 我想删除这个scaleType 属性。我知道我可以从代码中为 scaleType 添加不同的属性,但我不想将任何 scaleType 应用于此图像
  • 使用 imageViewObject.setScaleType(null);

标签: java android android-layout android-fragments android-xml


【解决方案1】:

如果你没有设置任何缩放类型,那么对于图像视图默认缩放类型是FIT_CENTER

//So to remove other scale types and set to default use
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

【讨论】:

  • 谢谢,我试试这个
【解决方案2】:

这是您在运行时从 Java 中设置或更改图像视图比例类型的方式:

ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setScaleType(ScaleType.CENTER);

您可以参考此documentation page 了解可能的 ScaleType 选项。

【讨论】:

  • 谢谢,我想删除这个scaleType 属性。通过这个,我可以从代码中为 scaleType 添加不同的属性,但我不想将任何 scaleType 应用于此图像
  • @Kirmani88 默认为 FIT_CENTER,因此如果您将其设置为该值,就好像您根本没有在 XML 中指定任何 ScaleType 一样
  • 您不能删除比例类型或不能设置为空。检查我的答案。您必须将其设置为默认的 FIT_CENTER。
猜你喜欢
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 2016-09-18
  • 2022-07-01
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多