您如何编程设置的TableRow内的ImageView的宽度?我不想单元格的宽度改变,细胞内的图像只有宽度。
布局:(省略号在那里,以减少code)的
< TableLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
<的TableRow安卓重力=center_vertical>
<按钮> ...< /按钮>
< ImageView的
机器人:ID =@ + ID /果
机器人:layout_width =120dp
机器人:layout_height =44dp
机器人:SRC =@可绘制/苹果
机器人:scaleType =矩阵
>< / ImageView的>
<按钮> ...< /按钮>
< /的TableRow>
<的TableRow> ...< /的TableRow>
< / TableLayout>
Java的:的
ImageView的IV =(ImageView的)findViewById(R.id.fruit);
的LayoutParams帧= iv.getLayoutParams();
frame.width = 100;
iv.setLayoutParams(架);
修改:我想要的图像被我设置,并有表格单元格是原来的宽度范围内冒出。下面是为我工作的感谢parag的建议在code例如:
BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.apple);
INT宽度= 50;
INT高= 44;
位图resizedbitmap = Bitmap.createBitmap(BMP,iv.getScrollX(),iv.getScrollY(),宽度,高度);
iv.setImageBitmap(resizedbitmap);
解决方案
,美调整位图的另一种方式。
ImageView的IMG =(ImageView的)findViewById(R.id.ImageView01);
BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.sc01);
INT宽度= 200;
INT高= 200;
位图resizedbitmap = Bitmap.createScaledBitmap(BMP,宽度,高度,真正的);
img.setImageBitmap(resizedbitmap);
How do you programmatically set the width of an ImageView inside a TableRow? I do NOT want the cell width to change, only the width of the image inside the cell.
Layout: (The ellipsis are there to reduce the code)
<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);
EDIT: i want the image to be cropped inside the bounds i set and have the table cell be the original width. Below is the code example that worked for me thanks to parag's suggestion:
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);
解决方案
Another way that u resized the bitmap
ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
int width=200;
int height=200;
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);