如何以編程方式設置TableRow中ImageView的寬度?我不希望單元格寬度改變,只有單元格內部圖像的寬度。Android:如何以編程方式設置ImageView的寬度TableRow
佈局:(省略號是有減少的代碼)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TableRow android:gravity="center_vertical">
<Button>...</Button>
<ImageView
android:id="@+id/fruit"
android:layout_width="120dp"
android:layout_height="44dp"
android:src="@drawable/apple"
android:scaleType="matrix"
></ImageView>
<Button>...</Button>
</TableRow>
<TableRow>...</TableRow>
</TableLayout>
的Java:
ImageView iv = (ImageView) findViewById(R.id.fruit);
LayoutParams frame = iv.getLayoutParams();
frame.width = 100;
iv.setLayoutParams(frame);
編輯:我想被裁剪圖像在我設置的邊界內,並使表格單元成爲原始寬度。下面是代碼示例,由於parag的建議,對我有效:
Bitmap bmp = BitmapFactory.decodeResource(getResources() , R.drawable.apple);
int width = 50;
int height = 44;
Bitmap resizedbitmap = Bitmap.createBitmap(bmp, iv.getScrollX(), iv.getScrollY(), width, height);
iv.setImageBitmap(resizedbitmap);
正是我所需要的 – keybee 2013-08-28 10:10:05