2

我希望 imgFirstAct 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 并没有变宽到背景图像的宽度。我应该怎么办?

非常感谢 :)

4

2 回答 2

0

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

于 2013-11-10T14:06:47.953 回答
0

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

 android:scaleType="fitXY"
于 2013-11-10T14:27:00.700 回答