我想创建一个ViewPager,其宽度换行到其内容,并在其父级中水平居中。第一个代码段使用LinearLayout创建此效果,如第一个屏幕截图所示。第二个代码片段是我尝试使用ViewPager而不是LinearLayout执行此操作,但结果不是所需的行为,如第二个屏幕截图所示。
关于如何创建第一个效果,但使用ViewPager的任何建议?
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setText("abcabcabcabcabc");
textView.setBackgroundColor(Color.YELLOW);
LinearLayout llayout = new LinearLayout(this);
llayout.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
llayout.setLayoutParams(layoutParams);
llayout.addView(textView);
layout = new RelativeLayout(this);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
layout.setBackgroundColor(Color.GREEN);
layout.addView(llayout);
setContentView(layout);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT, ViewPager.LayoutParams.WRAP_CONTENT));
textView.setText("abcabcabcabcabc");
textView.setBackgroundColor(Color.YELLOW);
ViewPager pager = new ViewPager(this);
pager.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
pager.setLayoutParams(layoutParams);
pager.setAdapter(new ViewPagerAdapter());
layout = new RelativeLayout(this);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
layout.setBackgroundColor(Color.GREEN);
layout.addView(pager);
setContentView(layout);
}
class ViewPagerAdapter extends PagerAdapter
{
@Override
public int getCount()
{
return 1;
}
public Object instantiateItem(ViewGroup collection, int position)
{
collection.addView(textView, 0);
return textView;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view)
{
collection.removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return (view==object);
}
@Override
public void finishUpdate(ViewGroup arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState()
{
return null;
}
@Override
public void startUpdate(ViewGroup arg0) {}
}
答案 0 :(得分:1)
如果您查看代码中的适配器名称 instantiateItem()
public Object instantiateItem(ViewGroup collection, int position)
{
collection.addView(textView, 0)
return textView;
}
您正在返回一个TextView作为页面,并且是您想要显示的唯一内容。
文档的相关部分如下: http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
一个非常简单的PagerAdapter可以选择使用页面视图本身 作为关键对象,从instantiateItem(ViewGroup,int)返回它们 在创建并将它们添加到父ViewGroup之后。匹配 destroyItem(ViewGroup,int,Object)实现会删除 从父ViewGroup和isViewFromObject(View,Object)查看 可以实现为return view == object;。
使用您想要的布局创建一个视图并将其返回到那里以获得您想要的效果。
见底部: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html