【问题标题】:How to align center an image view inside a view pager when the image's width is less than the display's?当图像的宽度小于显示器的宽度时,如何在视图寻呼机内对齐图像视图的中心?
【发布时间】:2015-11-18 12:48:30
【问题描述】:

我已经处理了几个小时了。我需要帮助。 我在 ViewPager 中有这个 ImageView。除了图像无法达到显示宽度外,一切都很好。我希望它水平居中,但无论我更改什么设置,我都无法使其工作。

我使用了布局参数,setRule,... 由于 ImageView 没有 setGravity,我应该选择什么方法?首先新建一个线性或相对布局,然后将图像视图添加到其中?没有更合理的方式让 ImageView 居中对齐吗?

这是我在 ViewPager 的适配器中的代码:

public Object instantiateItem(final ViewGroup container, final int position) {
    if (context == null)
        return null;
    final ImageView imageView = new ImageView(context);
    if (propertyImagesIDs == null || propertyImagesIDs.length == 0 || context == null) {
        return null;
    }
    new PropertyPhotoProxy().run(new Response.Listener<Photo>() {
        @Override
        public void onResponse(Photo photo) {
            imageView.setImageBitmap(photo.getBitmap());
            imageView.setScaleType(ImageView.ScaleType.FIT_START);
            ((ViewPager) container).addView(imageView, 0);

        }
    }, new DefaultErrorListener(), propertyImagesIDs[position]);

    return imageView;
}

这是图像在屏幕中的对齐方式,无论我更改什么设置。

【问题讨论】:

  • 为 ImageView 试试这个 android:scaleType="centerCrop"
  • 情况更糟。它裁剪图像并用裁剪后的图像填充整个父级(也裁剪和缩放)

标签: android android-layout imageview android-viewpager android-gravity


【解决方案1】:
 ImageView.ScaleType.FIT_START

不是你需要的。它保持纵横比并尝试在widthheight 之间的至少一个上填充dest。您可能想使用CENTER_INSIDECENTER_CROP

【讨论】:

  • 填满宽度或高度我都可以。我只希望它水平居中(如果它填满高度)
  • 居中裁剪,裁剪图像,center_inside,垂直和水平居中图像。我只想要水平。
猜你喜欢
  • 2018-06-05
  • 2014-11-26
  • 2017-09-22
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 2019-05-16
相关资源
最近更新 更多