我使用 ViewFlipper,我希望我的每个图像都能 match_parent。
但是当我添加这一行时我的程序关闭了。
ViewFlipper.LayoutParams params = new ViewFlipper.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
当我删除这一行时,应用程序运行,但图像无法匹配_parent....
这是我的代码:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Comic"
tools:showIn="@layout/comic">
<ViewFlipper
android:id="@+id/viewflipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
app:srcCompat="@drawable/st01_1"
android:scaleType="centerCrop" />
</RelativeLayout>
</ViewFlipper>
</FrameLayout>
这是 test.java:
viewflipper=(ViewFlipper)findViewById(R.id.viewflipper);
viewflipper.addView(getImageView(R.drawable.st01_1),1);
viewflipper.addView(getImageView(R.drawable.st01_2),2);
//
private ImageView getImageView(int id){
ImageView imageView = new ImageView(this);
FrameLayout.LayoutParams params = new
FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
imageView.setImageResource(id);
viewflipper.addView(imageView);
return imageView;
}
我该怎么办? 请帮助我,谢谢
最佳答案
像这样添加你的imageView。!您需要向 imageView 提供参数。
ImageView imageView = new ImageView(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
imageView.setImageResource(image);
viewFlipper.addView(imageView);
此外,删除 ViewFliper 的 LayoutParams
ViewFlipper.LayoutParams params = new ViewFlipper.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
关于java - 我想使用 LayoutParams 让 ViewFlipper MATCH_PARENT 但失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61199591/