【问题标题】:How to resize imageView to always match its image background dimensions in android?如何调整imageView的大小以始终匹配其在android中的图像背景尺寸?
【发布时间】:2013-11-10 13:29:01
【问题描述】:

我希望 imgFirstAct imageView 将其宽度调整为设置为它的背景宽度,只要背景比 imageView 更宽或更薄。

这是 imageView xml:

<ImageView
    android:id="@+id/imgFirstAct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imgCopyBelow"
    android:layout_alignLeft="@+id/imgCaloriesBelow"
    android:layout_alignRight="@+id/TextView04"
    android:layout_alignTop="@+id/imgCaloriesBelow"
    android:maxWidth="290dp" />

代码如下:

private ImageView imgFirstAct;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

          imgFirstAct = (ImageView)findViewById(R.id.imgFirstAct);

          imgFirstAct.setScaleType(ImageView.ScaleType.FIT_START);

          Bitmap bitmap = ((BitmapDrawable)imgCopyBelow.getBackground().getCurrent()).getBitmap ();

          int h = bitmap.getHeight();      

          Bitmap croppedBmp = Bitmap.createBitmap(bitmap, 0, 0, 120, h);    

          Drawable d = new BitmapDrawable(getResources(),croppedBmp);   

          imgFirstAct.setBackground(d);  
}

我在这里设置了一个比 imageView 更宽的图像,但是 imageView 并没有变宽到背景图像的宽度。 我该怎么办?

非常感谢:)

【问题讨论】:

    标签: android image imageview setbackground


    【解决方案1】:

    您应该在您的 Xml 布局文件中将 Padding 添加到 ImageView 中,然后通过 padding 使背景更宽。

    【讨论】:

      【解决方案2】:

      为了将图像设置为适合 imageview 的背景,您需要在 xml 中设置 scaletype,如下所示

       android:scaleType="fitXY"
      

      【讨论】:

      • 谢谢,ReshmaDB。我需要 imageView 来适应设置背景图像的宽度。如果背景图片比 imageView 宽,则 imageView 应该变宽。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      相关资源
      最近更新 更多