[android-developers] Re: ViewPager does not respect WRAP_CONTENT?
Hi,
-- Couldn't you just also add the linearlayout (with its textview) to the relative layout in (2) as you did in (1) ?
And specifically, add and remove the linear layout to the collection in your instantiate & destroy item methods instead of just the text view.
Regards
On Saturday, November 23, 2013 7:11:43 AM UTC+11, Ab wrote:
On Saturday, November 23, 2013 7:11:43 AM UTC+11, Ab wrote:
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?@Overrideprotected 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);}@Overrideprotected 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{@Overridepublic int getCount(){return 1;}public Object instantiateItem(ViewGroup collection, int position){collection.addView(textView, 0);return textView;}@Overridepublic void destroyItem(ViewGroup collection, int position, Object view){collection.removeView((View) view);}@Overridepublic boolean isViewFromObject(View view, Object object){return (view==object);}@Overridepublic void finishUpdate(ViewGroup arg0) {}@Overridepublic void restoreState(Parcelable arg0, ClassLoader arg1) {}@Overridepublic Parcelable saveState(){return null;}@Overridepublic void startUpdate(ViewGroup arg0) {}}![enter image description here][1]![enter image description here][2]
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
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 android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home