Android:如何以编程方式设置 TableRow 中 ImageView 的宽度

标签 android android-layout

如何以编程方式设置 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>

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);

最佳答案

调整位图大小的另一种方法

    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);

关于Android:如何以编程方式设置 TableRow 中 ImageView 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4811905/

相关文章:

java - 如何在应用程序初始化时初始化选项卡内容

android - 我可以更改 Android Layout Transition 的行为以消除淡入淡出但保持高度动画吗?

android - View 在布局中的份额相等

java - 为什么输入类型“numberDecimal | numberSigned”在Android Studio中不起作用

java - 游标的空列表问题

java - 没有 Java 的 Android

android - 如何使用兼容 Support Library 的 Android Material 组件?

java - 如何在后台继续收听 Android 上的推送通知

android - removeAllViews Android 上的动画

android - 如何从 EditText Android 获取值