为什么我的 ImageView 在 Android 中不会改变宽度?
Why my ImageView would not change width in Android?
我正在开发一个带有头像(图像图标)的 [=29=] 应用程序,ImageView 位于这样的 RelativeLayout 中
<ImageView
android:id="@+id/indicators"
android:layout_width="@dimen/conversation_avatar_size"
android:layout_height="@dimen/conversation_list_left_indicator_height"
android:layout_below="@id/contact_image"
android:layout_alignWithParentIfMissing="true"
android:layout_marginEnd="@dimen/smaller_def_margin"
android:layout_marginTop="@dimen/smaller_def_margin"
android:scaleType="fitCenter"
tools:src="@drawable/ic_reply"/>
但是,当我使 contact_image 不可见时,我无法更改此 ImageView 的宽度,因为指示器占用较少 space。代码如下
indicators.getLayoutParams().width = width;
indicatorsWidth = indicators.getWidth();
indicators.requestLayout();
在indicators.requestLayout();
之后,指标的宽度仍然保持原来的宽度不变。我想知道为什么会发生这种情况?我也试过 indicators.setLayoutParams(layoutParams);
但还是不行。
我确实发现了一些有趣的东西,尽管 indicators.getWidth()
仍然是旧值,indicators.getLayoutParams().width
现在是新宽度,并且 ImageView 大小根本没有改变。
更新:我找到了 view.getWidth()
是什么,然后我使用了 indicators.setLeft()
和 indicators.setRight()
,这次 indicators.getWidth()
也是新的宽度,但是大小仍然没有改变,我检查了一下,发现 indicators.getMeasuredWidth()
仍然是旧值,知道吗?
如果 ImageView
的高度是限制因素,您的问题可能是 android:scaleType="fitCenter"
。视图将调整大小以适应高度并通过调整宽度来保持纵横比。参见 this。
Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
这是一个玩这个概念的小应用程序。你会看到即使宽度改变了,高度也没有 android:scaleType="fitCenter"
。如果将其更改为 android:scaleType="fitXY"
,您将看到一个变化。这可能不是您的问题,但您可以使用此代码 MCVE 来测试一些想法。
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView indicators = findViewById(R.id.indicators);
indicators.postDelayed(new Runnable() {
@Override
public void run() {
indicators.getLayoutParams().width = 500;
indicators.requestLayout();
}
}, 2000);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/indicators"
android:layout_width="100px"
android:layout_height="100px"
android:layout_alignWithParentIfMissing="true"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
修改LayoutParams
时,必须将对象重新分配回视图,否则将没有效果。
ViewGroup.LayoutParams params = myView.getLayoutParams();
params.width = newWidth;
myView.setLayoutParams(params);
最后一行实际上改变了视图的宽度。
我正在开发一个带有头像(图像图标)的 [=29=] 应用程序,ImageView 位于这样的 RelativeLayout 中
<ImageView
android:id="@+id/indicators"
android:layout_width="@dimen/conversation_avatar_size"
android:layout_height="@dimen/conversation_list_left_indicator_height"
android:layout_below="@id/contact_image"
android:layout_alignWithParentIfMissing="true"
android:layout_marginEnd="@dimen/smaller_def_margin"
android:layout_marginTop="@dimen/smaller_def_margin"
android:scaleType="fitCenter"
tools:src="@drawable/ic_reply"/>
但是,当我使 contact_image 不可见时,我无法更改此 ImageView 的宽度,因为指示器占用较少 space。代码如下
indicators.getLayoutParams().width = width;
indicatorsWidth = indicators.getWidth();
indicators.requestLayout();
在indicators.requestLayout();
之后,指标的宽度仍然保持原来的宽度不变。我想知道为什么会发生这种情况?我也试过 indicators.setLayoutParams(layoutParams);
但还是不行。
我确实发现了一些有趣的东西,尽管 indicators.getWidth()
仍然是旧值,indicators.getLayoutParams().width
现在是新宽度,并且 ImageView 大小根本没有改变。
更新:我找到了 view.getWidth()
是什么,然后我使用了 indicators.setLeft()
和 indicators.setRight()
,这次 indicators.getWidth()
也是新的宽度,但是大小仍然没有改变,我检查了一下,发现 indicators.getMeasuredWidth()
仍然是旧值,知道吗?
如果 ImageView
的高度是限制因素,您的问题可能是 android:scaleType="fitCenter"
。视图将调整大小以适应高度并通过调整宽度来保持纵横比。参见 this。
Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
这是一个玩这个概念的小应用程序。你会看到即使宽度改变了,高度也没有 android:scaleType="fitCenter"
。如果将其更改为 android:scaleType="fitXY"
,您将看到一个变化。这可能不是您的问题,但您可以使用此代码 MCVE 来测试一些想法。
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView indicators = findViewById(R.id.indicators);
indicators.postDelayed(new Runnable() {
@Override
public void run() {
indicators.getLayoutParams().width = 500;
indicators.requestLayout();
}
}, 2000);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/indicators"
android:layout_width="100px"
android:layout_height="100px"
android:layout_alignWithParentIfMissing="true"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
修改LayoutParams
时,必须将对象重新分配回视图,否则将没有效果。
ViewGroup.LayoutParams params = myView.getLayoutParams();
params.width = newWidth;
myView.setLayoutParams(params);
最后一行实际上改变了视图的宽度。