【发布时间】:2019-01-15 20:42:10
【问题描述】:
我在 XML 文件中有一个相对布局,其中包含一个 ImageView 元素,其中包含图像的宽度和高度。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/tankHeight"
android:layout_width="210dp"
android:layout_height="350dp"
android:layout_centerInParent="true"
app:srcCompat="@drawable/tank_progress" />
然后我尝试在其类中调用该方法时动态更改图像的高度(只有高度,而不是宽度),如下...
public void updateTank() {
ImageView myTank = (ImageView)findViewById(R.id.tankHeight);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
myTank.getLayoutParams();
params.height = 350 - 35;
myTank.setLayoutParams(params);
}
但是,当我只想将高度降低 35 像素时,当调用此方法时,图像会在高度 和 宽度上按指定量缩放。我的理解是,这可能是由于使用了相对布局,但我不确定如何克服这个问题,以便我只能以编程方式更改高度。通过阅读this 和this,我认为该方法设置正确,所以不确定为什么它的显示与预期不同?
【问题讨论】:
标签: java android android-layout android-relativelayout layoutparams