TableRow内でImageViewの幅をプログラムで設定するにはどうすればよいですか?私は、セルの幅を変更する必要はありません、セル内の画像の幅のみ。Android:TableRow内でImageViewの幅をプログラムで設定する方法
レイアウト:(省略記号は、コードを減らすためにある)
<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>
ジャワ:
ImageView iv = (ImageView) findViewById(R.id.fruit);
LayoutParams frame = iv.getLayoutParams();
frame.width = 100;
iv.setLayoutParams(frame);
EDIT:私は画像をトリミングしたいです私が設定した範囲内でテーブルセルを元の幅にします。以下は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