ITKeyword,专注技术干货聚合推荐

注册 | 登录

Android ViewPager padding/margin between page fragments

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:android viewpager

viewpager 是一个好用,又华丽丽的控件。 1. 禁止viewpager 左右滑动,但不影响viewpager 每个paer上的事件 public class MainView extends ViewPager{ privat

Android Market/Google Music seem to be able to have a gap of some sort between the different fragments that are contained in the ViewPager.

Any idea how this is done? Adding margin/padding to the actual fragment view doesn't work, because the view still needs to occupy the entire width of the screen. The 'gap' is only visible when swiping the ViewPager.

android android-fragments android-viewpager
asked Sep 8 '11 at 5:29 psychotik 19.1k 26 79 130

 | 

5 Answers
5

In Support Package, r4 you can use these methods:

setPageMargin(int marginPixels)
setPageMarginDrawable(Drawable)
setPageMarginDrawable(int)

in ViewPager class.

I don't know why this doesn't have equivalent in XML :-(

If you need use dip unit you can use this method:

public static int convertDip2Pixels(Context context, int dip) {
    return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());
}

edited Oct 24 '12 at 16:06 Flynny75 1,143 9 9 answered Nov 1 '11 at 21:11 ATom 10.4k 4 30 41 12   For dp to px conversion, I prefer the cleaner Context.getResources().getDimensionPixelOffset(int dimenId) method. –  greg7gkb Jun 6 '13 at 22:10      getting resource not found when executing Context.getResources().getDimensionPixelOffset(int dimenId). The ID of teh dimension is correct but it doesnt work –  jonney Oct 28 '14 at 15:01      use adapter reference instead of Context –  Kaveesh Kanwal May 28 '15 at 17:08

 | 

Hoping this might help someone with a ViewPager and page margins.

I wanted to have a ViewPager is a CardView and had issues with the padding and the margin between the viewpager pages. The setPageMargin did not work for me at all. I had to use the following:

推荐:android Fragments:管理fragment

要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()。 你可以用FragmentManager来做以上事情:   1使用方法findF

int pagerPadding = 16;
pager.setClipToPadding(false);
pager.setPadding(pagerPadding, 0, pagerPadding, 0);

This allow the pages to be very close together.


answered Nov 17 '15 at 21:57 srayhunter 4,791 1 25 32

 | 

pager.setClipToPadding(false); pager.setPadding(left,0,right,0); If you need space between two pages in the viewpager then add pager.setPageMargin(int);


answered Sep 29 '16 at 11:28 RAHULRSANNIDHI 151 1 12

 | 

Try This .

    ViewPager viewPager = (ViewPager) findViewById(R.id.h_view_pager);
    viewPager.setClipToPadding(false);
    viewPager.setPadding(60, 0, 60, 0);
    viewPager.setPageMargin(20);
    ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setOffscreenPageLimit(adapter.getCount());
    viewPager.setAdapter(adapter);

answered Nov 21 '16 at 11:43 user2818364 1

 | 
  viewPager.setPageMargin(dpToPx(10)); 
  // replace 10 with the value you want for margin in dp. 


 public int dpToPx(int dp) {
    DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
    return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
 }

answered Apr 11 at 10:54 Anirudh 867 3 15 36

 | 

推荐:Android ViewPager

2013-11-27号补充: ViewPager切换动画PageTransformer使用,可以对viewpager进行动画了,而不用使用默认的方式,只要你能想得到动画的方式就可以了。 http://we


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。