【问题标题】:Unwanted white space beneath ImageView in ViewPager androidViewPager android中ImageView下方不需要的空白
【发布时间】:2016-07-15 14:08:13
【问题描述】:

我使用 ViewPager 作为图像滑块。我正在使用 Android Studio。一切正常,我可以在图像之间滑动。但是,由于某种原因,在我的图像下方,我有空白。我知道你不能在 ViewPager 上调用 wrap_content 并且我试图将包含图像滑块的片段放入一个 Activity 中,在图像滑块片段下方有另一个片段,但它们之间仍然是一些奇怪的空白区域我还可以滑动到另一个图像的空白,这当然只会在以后引起问题:

fragment_home.xml:

<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"
tools:context="HomeFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="ImageSliderFragment"
    android:id="@+id/fragment1"
    tools:layout="@layout/fragment_image_slider"
    android:layout_weight="2"/>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="BottomFragment"
        tools:layout="@layout/fragment_bottom"
        android:id="@+id/fragment2"
        android:layout_weight="2"/>

</LinearLayout>

</FrameLayout>

swipe_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
    android:id="@+id/image_view_of_slider"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"/>
<!-- adjustviewBounds prevents non-defined padding -->
<TextView
    android:id="@+id/text_view_of_swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"/>

</RelativeLayout>

fragment_image_slider.xml:

<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="wrap_content"
tools:context="ImageSliderFragment"
android:id="@+id/image_slide_id">

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager_of_image_slider"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<!--height="wrap_content/match_parent" both have the same result -->

</FrameLayout>

CustomSliderAdapter.java:

public class CustomSliderAdapter extends PagerAdapter {

private int[] image_res = {R.drawable.a,R.drawable.b,
        R.drawable.c, R.drawable.d};
private String[] image_names = {"a", "b", "c", "d"};
private Context ctx;
private LayoutInflater layoutInflater;

public CustomSliderAdapter(Context ctx) {
    this.ctx = ctx;
}

@Override
public int getCount() {
    return image_res.length;
}

@Override
public int getItemPosition(Object object) {
    return super.getItemPosition(object);
}

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

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.image_view_of_slider);
    TextView textView = (TextView) view.findViewById(R.id.text_view_of_swipe_layout);
    imageView.setImageResource(image_res[position]);
    textView.setText(image_names[position]);
    container.addView(view);

    return view;
}

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

ImageSliderFragment.class:

public class ImageSliderFragment extends Fragment {
ViewPager viewPager;
CustomSliderAdapter adapter;
View myView;
public static int imageHeight;


public ImageSliderFragment() {}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if(myView==null) {
        myView = inflater.inflate(R.layout.fragment_image_slider, container, false);
        viewPager = (ViewPager) myView.findViewById(R.id.view_pager_of_image_slider);
        adapter = new CustomSliderAdapter(getActivity());

        //With this I could set height of ViewPager manually
        ViewGroup.LayoutParams params = viewPager.getLayoutParams();
        params.height = imageHeight;
        viewPager.setLayoutParams(params);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setAdapter(adapter);
    } else {
        ((ViewGroup) myView.getParent()).removeView(myView);
    }

    return myView;
}

}

How activity looks

我还尝试使用数字 (f.e.200dp) 更改我的 xml 文件的高度,这很有效,但是有很多不同的设备屏幕,有时它会在某些时候搞砸

所以再次我的问题是:让 ViewPager 适合 Imageview,因为我也可以在空白处的图像之间滑动。 非常感谢您的帮助!

【问题讨论】:

  • android:layout_height="wrap_content" 到 android:layout_height="0dp" 的片段
  • @Pravin 在哪个布局中?顺便感谢您的帮助
  • fragment_home 布局
  • 它工作了几秒钟(但图像被压缩),但随后另一个片段(BottomFragment)占据了整个活动:O @Pravin,底部片段在一个奇怪的动画中向上滑动直到它占用整个活动。
  • 您的片段在垂直线性布局中同时具有 layout_height="wrap_content" 和 layout_weight="2" 。你想要哪一个?你不能两者兼得。

标签: android android-studio android-fragments android-viewpager android-pageradapter


【解决方案1】:

你可以在你的 imageview 上尝试这样的 viewpager 图片 ....

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:scaleType="fitXY" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多