gpt4 book ai didi

Android:ViewPager 不尊重 WRAP_CONTENT?

转载 作者:行者123 更新时间:2023-11-30 03:15:45 27 4
gpt4 key购买 nike

我想创建一个 ViewPager,其宽度环绕其内容,并在其父项中水平居中。第一个代码 fragment 使用 LinearLayout 来创建此效果,如第一个屏幕截图所示。第二个代码 fragment 是我尝试使用 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) {}
}

enter image description here

enter image description here

最佳答案

如果您查看适配器调用 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

A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int) after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.

使用您想要的布局创建一个 View 并将其返回到那里以获得您想要的效果。

见底部: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

关于Android:ViewPager 不尊重 WRAP_CONTENT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20153642/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com