【问题标题】:How to change imageview size on button click [duplicate]如何在按钮单击时更改图像视图大小[重复]
【发布时间】:2020-04-01 02:01:16
【问题描述】:

我正在开发一个在中心显示图像的应用程序。 ImageView的xml代码如下

<ImageView
    android:id="@+id/e_sym"
    android:layout_width="300px"
    android:layout_height="300px"
    android:rotation="0"
    android:contentDescription="@string/symbol_e"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.499"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.498"
    app:srcCompat="@drawable/symbol_e" />

我想通过单击按钮从java程序中更改高度和宽度。

decr.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //code to change height and width values of the ImageView
    }
});

尺寸必须为像素格式

【问题讨论】:

标签: java android xml


【解决方案1】:

你只需要在 LayoutParams 中提及 imageView 的宽度和高度即可。

decr.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //code to change height and width values of the ImageView

        imageView.getLayoutParams().height = 50; //can change the size according to you requirements
        imageView.getLayoutParams().width = 50; //--
        imageView.requestLayout()
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

    }
});

【讨论】:

    【解决方案2】:

    您必须在LayoutParams 中设置heightwidth。检查以下:

    decr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //code to change height and width values of the ImageView
    
            imageView.getLayoutParams().height = 20;
            imageView.getLayoutParams().width = 20;
            imageView.requestLayout()
        }
    });
    

    【讨论】:

    • @UzairMukadam,有什么问题?你为什么反转?我的回答有问题吗?指出我的错,以便我改正自己。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多