Android中不同宽度的布局在ViewPager中

前端之家收集整理的这篇文章主要介绍了Android中不同宽度的布局在ViewPager中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用Horizo​​ntal View Pager创建一些滚动视图.左视图必须具有全屏宽度,但只有四分之一宽度(它将是Dolphin浏览器中的垂直面板).有可能这样做吗?我在正确的布局中改变了 android:layout_width,但它没有用.

我的代码

  1. public class TestActivity extends FragmentActivity {
  2.  
  3. @Override
  4. public void onCreate(final Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main_view);
  7.  
  8. MyPagerAdapter adapter = new MyPagerAdapter();
  9. ViewPager pager = (ViewPager) findViewById(R.id.panelPager);
  10. pager.setAdapter(adapter);
  11. pager.setCurrentItem(0);
  12. }
  13. }

main_view.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical" >
  5.  
  6. <android.support.v4.view.ViewPager
  7. android:id="@+id/panelPager"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent" />
  10. </LinearLayout>

MyPagerAdapter.java

  1. public class MyPagerAdapter extends PagerAdapter {
  2.  
  3. @Override
  4. public int getCount() {
  5. return 2;
  6. }
  7.  
  8. @Override
  9. public Object instantiateItem(final View collection,final int position) {
  10.  
  11. LayoutInflater inflater =
  12. (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  13.  
  14. int resId = 0;
  15. switch (position) {
  16. case 0:
  17. resId = R.layout.left;
  18. break;
  19. case 1:
  20. resId = R.layout.right;
  21. break;
  22. }
  23.  
  24. View view = inflater.inflate(resId,null);
  25. ((ViewPager) collection).addView(view,0);
  26.  
  27. return view;
  28. }
  29.  
  30. @Override
  31. public void destroyItem(final View arg0,final int arg1,final Object arg2) {
  32. ((ViewPager) arg0).removeView((View) arg2);
  33.  
  34. }
  35.  
  36. @Override
  37. public boolean isViewFromObject(final View arg0,final Object arg1) {
  38. return arg0 == ((View) arg1);
  39.  
  40. }

left.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5.  
  6. <TextView
  7. android:id="@+id/textView1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="LEFT" />
  11.  
  12. </LinearLayout>

right.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="50dp"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:background="@color/light_blue" >
  6.  
  7.  
  8. <TextView
  9. android:id="@+id/textView2"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="RIGHT"/>
  13.  
  14. </LinearLayout>

解决方法

检查Murphy在 this question上的答案.你需要在PagerAdapter类上覆盖PagerAdapter的getPageWidth()方法,例如:
  1. @Override
  2. public float getPageWidth(int page) {
  3. if(page==0) {
  4. Display display = getWindowManager().getDefaultDisplay();
  5. Point size = new Point();
  6. display.getSize(size);
  7. return (float)LEFT_FRAGMENT_PIXEL_WIDTH / size.x;
  8. }
  9. else
  10. return super.getPageWidth(page);
  11. }

猜你在找的Android相关文章