I would like to create a ViewPager whose width wrap's to its contents, and
is centered horizontally in it's parent. The first code snippet uses a
LinearLayout to create this effect, as shown in the first screenshot. The
second code snippet is my attempt to do this with a ViewPager instead of
the LinearLayout, but the result is not the desired behavior, as shown in
the second screenshot.
Any suggestions as to how I create the first effect, but using a 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) {}
}
![enter image description here][1]
![enter image description here][2]
[1]: http://i.stack.imgur.com/6Ajm7.png
[2]: http://i.stack.imgur.com/qWYQY.png
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.