【问题标题】:Image View Fill Empty Space ViewPager Android图像视图填充空白空间 ViewPager Android
【发布时间】:2015-09-28 15:09:59
【问题描述】:

我一直在关注本教程Image Slide Using View Pager,但我无法使图像视图内视图寻呼机在水平和垂直方向上填充视图寻呼机内的完整空间。知道怎么做吗?

我已经在我的页面适配器中尝试过这个

mImageView.setAdjustViewBounds(true);
mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

但仍然没有运气:(

我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/img_slideshow_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:background="@drawable/img_border" >

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="150dp" />

    <TextView
        android:id="@+id/img_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/indicator"
        android:background="#88343434"
        android:ellipsize="end"
        android:paddingBottom="2dp"
        android:paddingLeft="5dp"
        android:paddingRight="2dp"
        android:paddingTop="2dp"
        android:singleLine="true"
        android:textColor="#ededed" />

    <apps.modisku.com.modisku.helper.CirclePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view_pager"
        android:padding="10dip" />
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp" >

<ImageView
    android:id="@+id/image_display"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/txt_image_slider" />

   </RelativeLayout>

我的页面适配器

public class ImageSlideAdapter extends PagerAdapter {
    ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options;
    private ImageLoadingListener imageListener;
    FragmentActivity activity;
    List<Product> products;
    HomeFragment homeFragment;

    public ImageSlideAdapter(FragmentActivity activity, List<Product> products,
                             HomeFragment homeFragment) {
        this.activity = activity;
        this.homeFragment = homeFragment;
        this.products = products;
        options = new DisplayImageOptions.Builder()
                .showImageOnFail(R.drawable.ic_error)
                .showStubImage(R.drawable.ic_launcher)
                .showImageForEmptyUri(R.drawable.ic_empty).cacheInMemory()
                .cacheOnDisc().showImageOnLoading(R.drawable.loadingicontransprant).build();

        imageListener = new ImageDisplayListener();
    }

    @Override
    public int getCount() {
        return products.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.vp_image, container, false);

        ImageView mImageView = (ImageView) view
                .findViewById(R.id.image_display);


       // mImageView.setLayoutParams(new ViewGroup.LayoutParams(mScreenWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        mImageView.setAdjustViewBounds(true);
        mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mImageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle arguments = new Bundle();
                Fragment fragment = null;
                Log.d("position adapter", "" + position);
                Product product = (Product) products.get(position);
                arguments.putParcelable("singleProduct", product);

                // Start a new fragment
                fragment = new ProductDetailFragment();
                fragment.setArguments(arguments);

                FragmentTransaction transaction = activity
                        .getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.promofragment, fragment,
                        ProductDetailFragment.ARG_ITEM_ID);
                transaction.addToBackStack(ProductDetailFragment.ARG_ITEM_ID);
                transaction.commit();
            }
        });
        imageLoader.displayImage(
                ((Product) products.get(position)).getImageURL(), mImageView,
                options, imageListener);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    private static class ImageDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                 }
               }
           }
        }
    }

【问题讨论】:

  • 你的浏览器里面有什么?它是片段还是简单视图?你能分享一下你的适配器代码吗
  • 你能告诉我下面的解决方案是否适合你吗?谢谢。

标签: java android android-layout android-viewpager


【解决方案1】:
    Use FrameLayout instead of RelativeLayout

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image_display"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:contentDescription="@string/txt_image_slider" />

       </FrameLayout>

【讨论】:

  • 如果这是您的,xml 将在片段中?
  • 嘿,你能分享一下你的适配器代码吗,我想试试这个。对我来说似乎很有趣的问题
  • 嘿,现在试试,实际上有 3 DP 的填充。我已经删除了它,现在图像会出现在屏幕上,我敢肯定!
  • 你为什么使用 pagerAdapter 为什么不使用 FragmentPagerAdapter ?
  • 也可以试试 mImageView.setScaleType(ImageView.ScaleType.FITXY);
【解决方案2】:

试试吧..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp" >

    <ImageView
        android:id="@+id/image_display"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:contentDescription="@string/txt_image_slider" />

</RelativeLayout>

希望对你有用。

【讨论】:

    猜你喜欢
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多