java 如何以编程方式设置 ViewPager layoutParams?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17831672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set ViewPager layoutParams programmatically?
提问by code511788465541441
I have the following code but I get an exception
我有以下代码,但出现异常
java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams 不能转换为 android.view.ViewGroup$MarginLayoutParams
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question_details_full_photo_view_pager);
Bundle bundle = getIntent().getExtras();
ArrayList<String> imageUrls = bundle.getStringArrayList("imageUrls");
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(this, imageUrls, true);
android.support.v4.view.ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager_full_photo);
android.support.v4.view.ViewPager.LayoutParams layoutParams = new LayoutParams();
layoutParams.width = LayoutParams.MATCH_PARENT;
layoutParams.height = LayoutParams.MATCH_PARENT;
viewPager.setLayoutParams(layoutParams);
viewPager.setAdapter(imagePagerAdapter);
}
回答by d4vidi
You really shouldn't be setting the layout params programmatically just for setting up width and height to "match_parent": this could easily be done using fundamental xml declarations.
您真的不应该仅仅为了将宽度和高度设置为“match_parent”而以编程方式设置布局参数:这可以使用基本的 xml 声明轻松完成。
If however there's something more advanced you need that you didn't specify - such as adjusting margins or adding views dynamically, consider wrapping the ViewPager with a layout matching your case. For example:
但是,如果您需要未指定的更高级的东西 - 例如调整边距或动态添加视图,请考虑使用与您的情况匹配的布局包装 ViewPager。例如:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager_full_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
In this case, to adjust margins (for example):
在这种情况下,要调整边距(例如):
ViewPager pager = (ViewPager) findViewById(R.id.view_pager_full_photo);
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) pager.getLayoutParams();
lp.topMargin += TOP_MARGIN_HEIGHT_PX;
etc.
等等。
Note that in this case the layout params returned by the call to pager.getLayoutParams() are those of the wrapping RelativeLayout- which could be more easily manipulated to your fit your needs.
请注意,在这种情况下,调用 pager.getLayoutParams() 返回的布局参数是包装的 RelativeLayout的布局参数- 可以更轻松地操作以满足您的需要。
回答by Antoine Marques
This is because you misunderstood the LayoutParams
in this context : setting the layout params of your ViewPager will layout it out in its parent which is a ViewGroup. You have therefore to pass an instance of ViewGroup.LayoutParams
.
If you want rather to lay out a component in your ViewPager, you have to set the layout params of the child with an instance of ViewPager.LayoutParams
这是因为您LayoutParams
在此上下文中误解了:设置 ViewPager 的布局参数会将其布局在它的父级 ViewGroup 中。因此,您必须传递ViewGroup.LayoutParams
.
如果您想在 ViewPager 中布置组件,则必须使用以下实例设置子组件的布局参数ViewPager.LayoutParams